Complete

WERETECH-12153

Issue has been resolved. 

The type checker warns about info[:field], even after checking :field is present.

The 4.0.7 release notes say:

 - Add type checking to ‘object[:variable]’ syntax

But now I get warnings from:

function getField(info as Object, field as Symbol) {
    if (info has field) {
        return info[field];
    }
    return null;
}

WARNING: <device>: bug.mc:3: Cannot determine if container access is using container type.

Since we've checked that the field exists, there shouldn't be a need to check the container type (and it could be a class, and not a container at all).

I get the same warning using a literal symbol (obviously I could rewrite this one as info.field):

function getField(info as Object) {
    if (info has :field) {
        return info[:field];
    }
    return null;
}

There doesn't seem to be a way to suppress this warning. The docs mention (:typechecker(options)), but I cant find any documentation for options - other than disableBackgroundCheck and disableGlanceCheck - and I haven't been able to guess an option that helps.

  • I don't have a development environment on this machine, so I cannot easily test this, but I'd like to think that you'd have some luck if you change the type information for the object parameter. Something more like this...

    class MyClass
    {
        var field1 as Number;
        var field2 as Float;
        
        function initialize(f1 as Number, f2 as Float) {
            field1 = f1;
            field2 = f2;
        }
        
        function getValue(obj as MyClass, sym as Symbol) as Number or Float or Null {
            if (obj has symbol) {
                return obj[symbol];
            }
            
            return null;
        }
    }

    I'm thinking that this has a chance of working without the warning because the compiler knows the type of obj here and it knows that it isn't an Array or Dictionary, which it seems is why it is complaining at you.

    If this doesn't work, I'm assuming it will make our compiler people unhappy.. probably with me for suggesting this in the first place.

  • The currently supported options are documented at the bottom of this page. Unfortunately, they can't be used to resolve the warning that you're seeing.

    https://developer.garmin.com/connect-iq/monkey-c/monkey-types/