How can I test at compile time whether T is an instance of an interface ?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Sep 23 19:16:13 UTC 2020


On Wed, Sep 23, 2020 at 07:08:47PM +0000, data pulverizer via Digitalmars-d-learn wrote:
> On Wednesday, 23 September 2020 at 18:56:33 UTC, wjoe wrote:
> > 
> > It doesn't occur to me that the compiler doesn't know at compile
> > time that
> > 
> > interface IFoo{}
> > class Foo: IFoo {}
> > 
> > class Foo implements interface IFoo.
> 
> Didn't think that the compiler didn't know but wasn't aware that you
> could use that information to statically dispatch. My mistake, I'll
> shut up now!

Of course the compiler knows. And of course it can use this information
for static dispatch. That's why D is so awesome at metaprogramming. ;-)

What the compiler *doesn't* know is whether a variable of some supertype
of Foo (say Object) implements IFoo, because that's something that can
only be determined at runtime (effectively, you need to downcast to Foo
/ IFoo and test if it's null).  But that's not what the OP is asking for
in this case.


T

-- 
Error: Keyboard not attached. Press F1 to continue. -- Yoon Ha Lee, CONLANG


More information about the Digitalmars-d-learn mailing list