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
  • Yes, I understand what is happening in the code now. My point is that the docs are worse than useless, they are actually incorrect.

    The Any type is special in the sense that it can be used in place of...any type.

    Directly contradicting the docs which state:

    the compiler will only allow values of that type to be assigned.

    Any is not Number. There is no caveat in this statement.

    As for the rest - yeah, I have turned on strict type checking and am converting my codebase to use it, although frankly at this point I do not trust the type checker to be correct based on the quality level in the rest of the platform.

    Separately, I have also noticed that the compiler is unable to verify initialization if the assignment happens anywhere other than in "initialize()":

    class Example {
      var thing as Float;
      function initialize() { setThing(1.0) }
      function setThing(v as Float) { thing = v; }
    }

    This doesn't compile as the compiler cannot verify that thing is initialized...No question here, just amusing.

    Thanks for all your help. Hopefully at some point Garmin or the Alpha Monkey comes to understand that when you invent a programming language, it's considered good practice to document how to use it, and keep these docs up to date and correct.

Comment
  • Yes, I understand what is happening in the code now. My point is that the docs are worse than useless, they are actually incorrect.

    The Any type is special in the sense that it can be used in place of...any type.

    Directly contradicting the docs which state:

    the compiler will only allow values of that type to be assigned.

    Any is not Number. There is no caveat in this statement.

    As for the rest - yeah, I have turned on strict type checking and am converting my codebase to use it, although frankly at this point I do not trust the type checker to be correct based on the quality level in the rest of the platform.

    Separately, I have also noticed that the compiler is unable to verify initialization if the assignment happens anywhere other than in "initialize()":

    class Example {
      var thing as Float;
      function initialize() { setThing(1.0) }
      function setThing(v as Float) { thing = v; }
    }

    This doesn't compile as the compiler cannot verify that thing is initialized...No question here, just amusing.

    Thanks for all your help. Hopefully at some point Garmin or the Alpha Monkey comes to understand that when you invent a programming language, it's considered good practice to document how to use it, and keep these docs up to date and correct.

Children
No Data