When class is cast to Any or Dynamic, toString() is not being called


I have an abstract class (let’s say MyClass) over some other class (let’s say TheOtherClass), and MyClass defines toString() method. The method is being called when a variable is of MyClass type. However when cast to Dynamic or Any, it seems that TheOtherClass.toString() is being called instead.

Example on Try Haxe where I used as TheOtherClass.

Is this behaviour to be expected (maybe because Float32Array is Haxe std class?) or am I missing something in declaration of toString() method?

(Valentin Lemière) #2

Abstracts don’t exist at runtime, so at runtime the object is of type TheOtherClass, that’s why it is that toString that is called.

Though if at compile time the type is of MyClass and not Any then the compiler can insert the correct function.