Typed Array casting

212 views
Skip to first unread message

David Mouton

unread,
Nov 19, 2012, 11:41:58 AM11/19/12
to haxe...@googlegroups.com
Hi,
 I want to test the type of an anonymous object, supposed to be an Array<Order>

I try this :
if(Std.is(data, Array<Order>) ){
               
Log.trace("return ok");
           
} else {
               
Log.trace("type error : " + message.data);
           
}

But the compiler is not happy and says :
characters 31-32 : Unexpected )

Any idea ?

Simon Krajewski

unread,
Nov 19, 2012, 11:49:05 AM11/19/12
to haxe...@googlegroups.com
Type parameters are not allowed in expressions. The parser will read this as a < binop between Array and Order, and then a > binop between that and ), which makes no sense grammar-wise.

In general, type parameter information is not available at runtime across platforms. You can work around this in several ways, e.g. by testing the type of data[0] against Order after confirming that data is an Array of non-zero length.

Simon

Juraj Kirchheim

unread,
Nov 19, 2012, 11:51:44 AM11/19/12
to haxe...@googlegroups.com
Type parameters are lost at runtime, with the exception of classes implementing `haxe.rtti.Generic` (and types marked as @:generic under Haxe 3).

Please note that it is also syntactically impossible. The easiest way to do it is to have a typedef for the given type, e.g.:

     typedef IntFastList = haxe.FastList<Int>;

     trace(Std.is(someValue, IntFastList));

The same is valid Haxe code for arrays as well, but checking an array of strings against IntArray would still yield true, because Array is not Generic.

Regards,
Juraj
Reply all
Reply to author
Forward
0 new messages