Nested Dictionary typechecking

Hi, i have the following json structure and want to get rid of the typechecking warning:

        var myDict = { 
            "version" => "1.1.1", 
            "generator" => "logger", 
            "data" => [ { 
                "uuid" => "49f54392-1f4a-11e9-ab14-d663bd873d93", 
                "last" => 1671960103000l, 
                "interval" => -1, 
                "protocol" => "omv", 
                "tuples" => [ [ 1671960103000l, 20095964 ] ]
            } ]
        };
        var vers = myDict.get("version");
        var draw = myDict.get(["data"][0]["tuples"[0][1]]);

WARNING: ... \WebRequestDelegate.mc:13,8: Cannot determine if container access is using container type.

This looks similar to CIQQA-1327 but i can't explore the correct typedef syntax.

  • instead of the unsearchable "CIQQA-1327" can you link the ticket (bugreport forum thread)?

  • Hi Former Member,

    The type checker does not actually infer container types.

    var myDict = { ... }

    This code results in creating a local variable assigned to a value of type Toybox.Lang.Dictionary<Any, Any>. There are a couple of ways to remove the warning. You can use a brute-force approach and type cast (which kind of defeats the purpose of the type checker). Or you can type the dictionary:

    import Toybox.Lang;
    
    function foo() as Void (
        var myDict =
            { ...} as Dictionary<String, String or Array<Dictionary<String, String or Number or Long or Array<Array<Number or Long>>>>>;
        ...
        return;
    }

    Then, you would want to use if-splitting since Toybox.Lang.Dictionary::get returns a poly type of null (if the key value does not exist) or the original container type:

            var d1 = myDict.get(\"data\");
            if ((d1 != null) && (d1 instanceof Array)) {
                var d2 = d1[0][\"tuples\"];
                if ((d2 != null) && (d2 instanceof Array)) {
                    var draw = d2[0][1];
                }
            }

    I hope this helps and let me know if that wasn't the solution you were looking for,

    Marianne 

  • Thanks for the explanation, as a hobbyist i would prefer to ignore the warning in favour saving 6 * x lines of code.