Discussion:
How to check if class implements a certain interface?
Danny Wilson
2007-05-16 12:10:37 UTC
Permalink
---
--- I think this one got lost, so i'm resending it ---
---

Hi Nicolas,

I'm trying to figure out how to properly check if a class implements an
interface...

interface Bla {
public var x : String;
}

class Test implements Bla
{
public var x : String;
public function new(){}
public static function main()
{
switch(Type.typeof(new Test())){
case TClass(c):
var a : Array<Dynamic> = untyped c.__interfaces__;
for(i in a) if(i == Bla) trace('yay');
default:
}
}
}

This works, but a simple "new Test() instanceof Bla" would be much
prettier ;-)
--
haXe - an open source web programming language
http://haxe.org
Nicolas Cannasse
2007-05-16 13:19:06 UTC
Permalink
Post by Danny Wilson
This works, but a simple "new Test() instanceof Bla" would be much
prettier ;-)
Std.is(new Test(),Bla)

Nicolas
--
haXe - an open source web programming language
http://haxe.org
Danny Wilson
2007-05-16 13:32:01 UTC
Permalink
Ah, ofcourse.
Didn't think of looking in the Std class. Maybe it's more obvious to have
it moved inside Type ( Type.is() )?

Thanks :-)
Post by Nicolas Cannasse
Post by Danny Wilson
This works, but a simple "new Test() instanceof Bla" would be much
prettier ;-)
Std.is(new Test(),Bla)
Nicolas
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
Nicolas Cannasse
2007-05-16 14:12:08 UTC
Permalink
Post by Danny Wilson
Ah, ofcourse.
Didn't think of looking in the Std class. Maybe it's more obvious to have
it moved inside Type ( Type.is() )?
"Type" is quite big and "is" is useful in a lot of cases so it's better
to keep it as part of Std in order to minimize dependencies.

Nicolas
--
haXe - an open source web programming language
http://haxe.org
Loading...