Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haxe Reflection - Subclasses and Interfaces

I can use the Haxe Type Class to reflect an object's class e.g.

getClass<T> (o:T):Class<T>

Is there a way to check whether a given object implements an interface or is a subclass of another class?

like image 829
Matthew Molloy Avatar asked Mar 16 '23 12:03

Matthew Molloy


1 Answers

You can use Std.is:

class Subclass extends OriginalClass implements IMyInterface {}

var myObj = new Subclass();

var isClass = Std.is(myObj, OriginalClass);      // returns true
var isSubclass = Std.is(myObj, Subclass);        // also returns true
var isInterface = Std.is(myObj, IMyInterface);   // also returns true

Will return "true" if the second argument is the class of the object, one of its parent classes, or an interface it implements.

like image 53
heyitsbmo Avatar answered Mar 21 '23 02:03

heyitsbmo