Acknowledged
CIQQA-3455

Linter expects a Number (only) as argument for the Dict.hasKey() method for -l1 and above

Looks like I can't post code, nor images, nor anything. So you will have to live with my description:
When retrieving a dictionary from the storage:
var foo = Toybox.Application.Storage.getValue("foo")
and using
foo.hasKey("bar")
on said dictionary, the linter expects a number only as argument and the code fails to compile.
Error message:
Invalid '$.Toybox.Lang.String' passed as parameter 1 of type '$.Toybox.Lang.Number'.
  • I do agree that the error doesn't make any sense, and it's not the first time that the default type-check level (-l 1) is materially worse than the higher type check levels. (Yeah I know it's on purpose.)

    In a perfect world, -l 2 would be the default.

  • Sorry the forum won't let me type in the actual code but here it is again.

  • TL;DR: cast the return type of getValue and the error will go away:
    Ofc the problem here is that nothing prevents you from casting to the wrong type - i.e. a type which is not returned by getValue[] at all, or a type that doesn't match the value you actually stored in Storage.
    [4/x]
  • The compiler doesn't know which type will be returned by Application.Storage.getValue[], so it assumes it could be any of the above types. The real problem is trying to call a function [hasKey] which doesn't exist on all of those types.

    So the solution is to cast the return value of getValue to the type that you know it will be. I hate casts in Monkey C [because they're inherently unsafe), but sometimes they're unavoidable.
    [3/x]
  • WARNING: Cannot find symbol ':hasKey' on type 'Null'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.Array<Null or $.Toybox.Lang.Array<$.Toybox.Application.PropertyValueType> or $.Toybox.Lang.Dictionary<$.Toybox.Application.PropertyKeyType,$.Toybox.Application.PropertyValueType> or $.Toybox.WatchUi.BitmapResource or $.Toybox.Lang.Boolean or $.Toybox.Lang.Char or $.Toybox.Lang.Double or $.Toybox.Lang.Float or $.Toybox.Lang.Long or $.Toybox.Lang.Number or $.Toybox.Lang.String>'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.WatchUi.BitmapResource'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.Char'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.Double'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.Boolean'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.Float'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.Long'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.Number'.
    WARNING: Cannot find symbol ':hasKey' on type '$.Toybox.Lang.String'.
    ERROR Invalid '$.Toybox.Lang.String' passed as parameter 1 of type '$.Toybox.Lang.Number'.
    [2/x]