class Fruit { public function new() { }}
class Apple extends Fruit { public var name = 'apple';}class Pear extends Fruit { public var name = 'pear';}
class Test { static function main() { var fruits = new Array<Fruit>(); fruits.push(new Apple()); fruits.push(new Pear());
trace(cast(fruits[0], Apple).name); }}
foo.getFruit<Apple>();
Ah 'is' but for JS target that seems to generate even more code than getClass or atleast pretty much the same stuff, I think a switch with it would be better and if you have to use using then do it on 'Std', the 'as' static just adds another function with little gain.
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
--- You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.
In the end I'm hoping to get something as close as possible to C# to a generic return type in C# like this:
foo.getFruit<Apple>();
class Fruit{ public function new() { }
public function getFruit<T:Fruit>(cls:Class<T>):T { return Std.is(this, cls) ? cast this : throw 'Not a ${Type.getClassName(cls)}'; }}
var f:Fruit = new Apple();
trace(f.getFruit(Apple).name);