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

Otávio Augusto otavio.porto at fatec.sp.gov.br
Thu Sep 24 00:33:08 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.

see if "import std.traits: isImplicitlyConvertible" helps you.



More information about the Digitalmars-d-learn mailing list