forum > haxe.Type(). Getting type of "T"
-
Grzegorz Nowak Feb 13 at 21:31
Is there a way fetching type of "T" in the base class, like this:
class Container<T : AbstractItem> { public function showT() : String { return Type.getClassName(Type.getClass } } var c1 : Container<ConcreteItem> = new Container(); var c2 : Container<EvenMoreConcreteItem> = new Container(); trace(c1.showT()); // should print "ConcreteItem" trace(c2.showT()); // should print "EvenMoreConreteItem"
Obviously the above won't comple.
-
Working with T can be very tricky the best place is to look through some of the standard library for clues. I would suggest maybe trying to create an empty instance of T to check it's type.
trace( Type.getClassName( Type.createEmptyInstance( Class<T> ) );
but I seem to remember this being really tricky to achieve, so I doubt if my guess is useful, the other approach might be rtti. -
Grzegorz Nowak Feb 17 at 22:09
Tried similar.
Invoking this pattern:method(Class<T>);
results in
unexpected )
at code compilation -
Grzegorz Nowak Feb 17 at 22:37
A detour might look like this:
class Base<T> { teeClass : Class<T>; public function new( teeClass : Class<T>) { this.teeClass = teeClass; } public function getTeeClassName() : String { return Type.getClassName(teeClass); } public static main() { var a : Base<TypeA> = new Base(TypeA); trace(a.getTeeClass()); } }
I've used similar pattern in my code and it kind'a did the job.
< Prev
| Page 1 / 1 |
Next >