With the current compiler (sdk-4.1.5 and earlier),
import Toybox.Lang; function foo(x as Number or String) as Boolean { return x instanceof Number; }
compiles, even with strict type checking, but fails at runtime (Could not find symbol 'Number'
). Note that x as Number
is fine, but x instanceof Number
fails.
With all the compiler2 beta releases, the above compiles, and runs as expected with the default options (in fact, this was explicitly called out as one of the highlights of compiler 2, in this announcement). But at -O0, it fails at runtime, in the same way that the sdk-4.1.5 compiler does (again, with all three beta releases of the compiler2 tool chain).
This means that code that works at -O1 and -O2, and that type checks at -O0, is going to fail at runtime at -O0.
I think the change to import makes sense; but having it only apply at -O1 and higher seems like a mistake - its going to be possible to write code that only works with optimization enabled...