Invalid 'String' passed as container index of type 'Number'

I get these errors with type checking whenever I do `x["str"]` where `x` is a dictionary.

Anyone know why, and how to fix?

  • How is x defined?

    If it's a member variable, try something like this (change Number to whatever your actual value type is):

    var x as Dictionary<String, Number> = { "a" => 1, "b" => 2 } as Dictionary<String, Number>;

    If it's a local variable, try this:

    var x = { "a" => 1, "b" => 2 } as Dictionary<String, Number>;

  • Thanks.

    Type inference and checking in Monkey C seems quite incomplete and buggy.

    This code fails to type check: `dc.fillPolygon([[dotX - 3, dotY], [dotX, dotY + 4], [dotX + 3, dotY]])` with this error: `Invalid '$.Toybox.Lang.Array<Any>' passed as parameter 1 of type '$.Toybox.Lang.Array<$.Toybox.Lang.Array<$.Toybox.Lang.Double or $.Toybox.Lang.Float or $.Toybox.Lang.Long or $.Toybox.Lang.Number>>'`.

    and adding `as Lang.Array<Lang.Array<Lang.Numeric> >` does not fix it. I guess I have to add `as` to each element of the outer array?

    I will just disable type checking again until it's implemented better.

  • Type inference and checking in Monkey C seems quite incomplete and buggy.

    Yep it's a pain.

    This code fails to type check: `dc.fillPolygon([[dotX - 3, dotY], [dotX, dotY + 4], [dotX + 3, dotY]])` with this error: `Invalid '$.Toybox.Lang.Array<Any>' passed as parameter 1 of type '$.Toybox.Lang.Array<$.Toybox.Lang.Array<$.Toybox.Lang.Double or $.Toybox.Lang.Float or $.Toybox.Lang.Long or $.Toybox.Lang.Number>>'`.

    and adding `as Lang.Array<Lang.Array<Lang.Numeric> >` does not fix it. I guess I have to add `as` to each element of the outer array?

    It's worse than that as types nested more than 1 deep (e.g. Array<Array<Number>>) aren't recognized by the compiler/parser (although they're required for stuff like this). The workaround is to do a typedef. See my comment here.

        typedef ArrayOfNumbers as Array<Number>;
        function testFill(dc as Dc) {
            var hrx = 5;
            var y = 2;
            dc.fillPolygon(
                [
                    [hrx - 3, y + 2] as Array<Number>,
                    [hrx, y] as Array<Number>,
                    [hrx + 3, y + 2] as Array<Number>,
                    [hrx - 3, y + 2] as Array<Number>
                ] as Array<ArrayOfNumbers>
            );
        }