Let’s take a look. I pasted your code into try.haxe.org, hit “Build + Run,” and looked at the JS source:
var a = null;
console.log("a = " + Std.string(_$UInt_UInt_$Impl_$.toFloat(a)));
console.log("a == 0 -> " + Std.string(a == 0));
As you can see, a
is untyped when first created. Then in line 2, it calls Std.string(UInt.toFloat(a))
. It does this because that’s how UInt.toString()
is implemented:
However, UInt.toInt()
is a bit different:
It just returns the value unchanged, because it thinks it’s already an integer. So if it’s null, that’s what gets returned.
Seeing that, I decided to add one final line of Haxe:
trace("a == 0.0 -> " + (a == 0.0));
That gets converted to the following in JS:
console.log("a == 0.0 -> " + Std.string(_$UInt_UInt_$Impl_$.toFloat(a) == 0.0));
With the following output:
a == 0.0 -> true
That isn’t exactly what happened here. What happened here is that Javascript is untyped. It just doesn’t care about type.
I went back to try.haxe.org and set the target to “SWF.” It gave this error when I tried to compile:
Test.hx:3: characters 23-27 : On static platforms, null can't be used as basic type UInt
So yeah, it’s all because this is Javascript.