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
  • Type checking in Monkey C is purely a compile-time thing, just like TypeScript and Python. Contrast with C, C++, Java, Swift, etc, where type casts can actually make a difference in a value/type at runtime.

    Therefore, the only purpose of a type cast is to override the type checker's idea of the type of a variable/value. A cast can *never* change the actual run-time type (or value) of a variable. The correct code would be:

    var myNumber = myFloat.toNumber() 

    Btw,  this is why type casting is so dangerous in Monkey C, and I why I always suggest avoiding it unless absolutely necessary.

    It doesn't help that:

    - the syntax for type declarations (which are good) is exactly the same as the syntax for type casts (which are dangerous)

    - unlike TypeScript, Monkey C doesn't give you any warnings/errors if you try to cast between 2 unrelated types

Comment
  • Type checking in Monkey C is purely a compile-time thing, just like TypeScript and Python. Contrast with C, C++, Java, Swift, etc, where type casts can actually make a difference in a value/type at runtime.

    Therefore, the only purpose of a type cast is to override the type checker's idea of the type of a variable/value. A cast can *never* change the actual run-time type (or value) of a variable. The correct code would be:

    var myNumber = myFloat.toNumber() 

    Btw,  this is why type casting is so dangerous in Monkey C, and I why I always suggest avoiding it unless absolutely necessary.

    It doesn't help that:

    - the syntax for type declarations (which are good) is exactly the same as the syntax for type casts (which are dangerous)

    - unlike TypeScript, Monkey C doesn't give you any warnings/errors if you try to cast between 2 unrelated types

Children
No Data