Acknowledged

bug: vscode extension warns about possible nulls where it's not possible

Why I get this yellow wave line under the comparsion? I thought this was already fixed long ago (though that might have been in the compiler, not the vsc extension)
both with SDK 7.4.3 and 8.0.0-Beta
  • > It seems that the actual problem has nothing to do with local vs non-local variables, but with the presence of any kind of explicit scope specifier ($, self, class name, module name).

    Then again I still see a variant of this problem with a static member variable (no self prefix), so I guess this is a bit more complex than I assumed (or there are in fact multiple problems)

    forums.garmin.com/.../type-narrowing-still-doesn-t-work-for-non-locals-e-g-global-variables-or-member-variables

  • Or I could be 100% wrong and it's actually a bug, as it seems type narrowing of non-locals works in *some* cases.

    e.g.

    It seems that the actual problem has nothing to do with local vs non-local variables, but with the presence of any kind of explicit scope specifier ($, self, class name, module name).

  • I would argue that this is a more of a limitation / missing feature than a bug (as in code that's supposed to work one way but actually works a different way). (Yes it's a bug from our pov ofc).

    The type checker apparently just doesn't have the ability to narrow / infer the type of anything other than a local variable. And I think it's possible in theory (obviously typescript is able to do so)

    As this example shows, typescript:

    - can narrow the type of a non-local variable (good)

    - but it doesn't notice that a non-local variable's type can be changed by other code after narrowing (bad). In this sense, Monkey C is actually safer than TypeScript

  • Example of rewrite which would work as expected.

    (I am NOT suggesting that this workaround is reasonable or that anyone is obligated to use it).

  • Yes, it's the same issue as before:

    https://forums.garmin.com/developer/connect-iq/i/bug-reports/type-narrowing-still-doesn-t-work-for-non-locals-e-g-global-variables-or-member-variables

    Type narrowing doesn't work for non-local variables. If you rewrite the code with intermediate local variables, then it works as expected.

    I understand the limitation here - it's similar to why types can only be inferred for local variables: because the compiler can't always know that you aren't assigning some value to a global or member variable that reset its idea of what the type is.

    But I would argue that the compiler can and should be smarter in cases like the examples you have given.