Acknowledged
CIQQA-3581

Casting Float to Number does not yield Number

This code:

var myFloat = 5.0;
var myNumber = myFloat as Number;
var mod = myNumber % 1;

Compiles without warnings, but crashes at runtime with:

Error: Unhandled Exception
Exception: UnexpectedTypeException: Expected Number/Long, given Number/Float

As far as I can tell:

  • this behavior is undocumented
  • the types "Number/Long" and "Number/Float" are undocumented
  • the effects of casting numeric types are undocumented

Consequently I'm not sure how to characterize this bug. If it's expected, this is a documentation bug. If it's not expected, this is a runtime and/or compiler bug.

I read the relevant section on "type casting" in the SDK docs. I've reproduced the totality of the documentation on type casting here:

The as keyword can also be used in an expression to type cast a value to another type. This can be useful if the type is not clear to the type system.

This, unfortunately, does not help very much.

---

Tested on Descent G1 & G2 simulators

SDK 8.3.0

Parents
  • - You can remove this loophole by increasing the type checking level

    -- the default type checking level is 1 / gradual. In this mode, variables, arguments and return values are allowed to be untyped without any warnings or errors. The exception is that if you are passing a function as a callback to an API call, then that function needs to have the correct type)

    -- if you increase the type checking level to 2 / informative or 3 / strict, then you will be given a warning/error about untyped variabes

Comment
  • - You can remove this loophole by increasing the type checking level

    -- the default type checking level is 1 / gradual. In this mode, variables, arguments and return values are allowed to be untyped without any warnings or errors. The exception is that if you are passing a function as a callback to an API call, then that function needs to have the correct type)

    -- if you increase the type checking level to 2 / informative or 3 / strict, then you will be given a warning/error about untyped variabes

Children
No Data