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

data pulverizer data.pulverizer at gmail.com
Wed Sep 23 18:49:28 UTC 2020


On Wednesday, 23 September 2020 at 18:37:45 UTC, wjoe wrote:
> I have some similar functions:
>
> void register(C: IFoo)()
> {
>   _insert!C();
> }
>
> void register(C)() if (behavesLikeFoo!C)
> {
>   _insert!C();
> }
>
> There are more overloads with parameters so I want to merge them
>
> void register(C, ARGS...)(ARGS args) if (behavesLikeFoo!C || 
> isInstanceOf!(C, IFoo))
> {
>   _insert!C(args);
> }
>
> I found a lot of information on how to do this at runtime but 
> not at compile time.
> std.traits: isInstanceOf doesn't work. Neither did anything I 
> tried with typeid etc.
>
> The behavesLikeFoo constraint works as expected but it accepts 
> any class no matter whether or not it implements the interface.

A class at compile time is it's own static type, OOP polymorphism 
is a runtime feature not compile time. You have to write your own 
traits for specific objects to get them to relate to each other 
using static overloading.


More information about the Digitalmars-d-learn mailing list