How to find a type of a variable?

Is there a better way of finding out a type of variable except something like

(o == null ? "null" : (o instanceof Lang.String ? "String" : (o instanceof Lang.Object ? "Object" : "?"))) ...


Thanks!
  • There aren't many times that I don't know the type of an object, but when there's a question, I use instanseof. Checking that what's expected to be a Number is a Number with app settings is one case (see the New Developer FAQ point 10 for an example)

    Maybe a bit more about what you want to be able to do?
  • I just wanted a debug function (for example, to find out what I pulled from Object storage).
  • Somewhere I think someone posted a function to show the object type and value, but I'm not sure were or what it was in relation to, but if I recall, it was a series of if/else if statements with instanceof checks.
  • Travis sent me this which I find very useful for debugging.

    Of course, the Forum won't let me post it.

  • I wanted to try a code block with the .txt RaceQs posted. I'd heard it should be working. RaceQs - what error were you getting when you tried to post?

    function type_name(obj) {
        if (obj instanceof Toybox.Lang.Number) {
            return "Number";
        } else if (obj instanceof Toybox.Lang.Long) {
            return "Long";
        } else if (obj instanceof Toybox.Lang.Float) {
            return "Float";
        } else if (obj instanceof Toybox.Lang.Double) {
            return "Double";
        } else if (obj instanceof Toybox.Lang.Boolean) {
            return "Boolean";
        } else if (obj instanceof Toybox.Lang.String) {
            return "String";
        } else if (obj instanceof Toybox.Lang.Array) {
            var s = "Array [";
            for (var i = 0; i < obj.size(); ++i) {
                s += type_name(obj);
                s += ", ";
            }
            s += "]";
            return s;
        } else if (obj instanceof Toybox.Lang.Dictionary) {
            var s = "Dictionary{";
            var keys = obj.keys();
            var vals = obj.values();
            for (var i = 0; i < keys.size(); ++i) {
                s += keys;
                s += ": ";
                s += vals;
                s += ", ";
            }
            s += "}";
            return s;
        } else if (obj instanceof Toybox.Time.Gregorian.Info) {
            return "Gregorian.Info";
        } else {
            return "???";
        }
    }

  • Yes, the code block posting JSON errors should be resolved... we've been watching to see how many more people are seeing it.

  • I got (and still get)
    Error Loading Preview
    from the Preview button. Assumed the worst so bailed out.
  • It looks like some text fails to preview, but actually posts properly.

    The following text causes an error message when I press "Preview", but is still saved when I press "Post Reply". If I add a space between "735" and "xt", preview starts to work.

    235 and 735xt
  • Brandon.ConnectIQ, looks like there are still problems posting code (or certain combos of numbers and symbols).

    Now the forum behaviour looks like this to me, when you post/preview:
    1. Both preview and post work.
    2. Preview fails, but post works.
    3. Preview fails and post fails. The easiest way to test this without posting is to leave the CAPTCHA blank and press Post Reply. You will get the familiar error message "Error while saving content: SyntaxError: Unexpected token A in JSON at position 1" instead of "The string you entered for image verification did not match what was displayed."

    Example of code that won't post (remove the space between "al" and "ert"):
    al ert(


    Also, using "@-mentions" can prevent posts from being submitted.

    When I initially mentioned you, typing @Bran... and using the autocomplete function to finish it (which leaves the whole thing as a link), the post wouldn't submit. It only worked when I re-typed the whole thing as plain text. In fact, when I edit the post afterwards, with the mention displayed as a link, it won't submit. I have to delete the mention and retype it as plain text.

    Might just be that links have issues in general, but I haven't played around with it enough.
  • Former Member
    Former Member over 4 years ago in reply to jim_m_58

    Thanks for posting this. The Array and Dictionary code needed some tweaking. Here's my edited version:

    using Toybox.Lang;
    
    function type_name(obj) {
        if (obj instanceof Toybox.Lang.Number) {
            return "Number";
        } else if (obj instanceof Toybox.Lang.Long) {
            return "Long";
        } else if (obj instanceof Toybox.Lang.Float) {
            return "Float";
        } else if (obj instanceof Toybox.Lang.Double) {
            return "Double";
        } else if (obj instanceof Toybox.Lang.Boolean) {
            return "Boolean";
        } else if (obj instanceof Toybox.Lang.String) {
            return "String";
        } else if (obj instanceof Toybox.Lang.Array) {
            var s = "Array [";
            for (var i = 0; i < obj.size(); ++i) {
                s += type_name(obj[i]);
                s += ", \n";
            }
            s += "]";
            return s;
        } else if (obj instanceof Toybox.Lang.Dictionary) {
            var s = "Dictionary {\n";
            var keys = obj.keys();
            var vals = obj.values();
            for (var i = 0; i < keys.size(); ++i) {
                s += keys[i];
                s += ": ";
                s += vals[i];
                s += ", \n";
            }
            s += "\n}";
            return s;
        } else if (obj instanceof Toybox.Time.Gregorian.Info) {
            return "Gregorian.Info";
        } else {
            return "???";
        }
    }

    You can put that in a .mc file in your source folder, and call it like:

    using Toybox.System;
    
    System.print("x = " + type_name(x));