www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I make a template function that only accepts instances of a

reply Jack <jckj33 gmail.com> writes:
isInstanceOf from std.traits seems to not work with class the way 
I need to. I'd like to make a template function accepts only 
class of a specified class type

class A { }
class B : A { }
class C : A { }


import std.traits : isInstanceOf;

int f(T)(in A[int] arr)
if(isInstanceOf!(A, T)) // doesn't work for class from what I saw
{
   foreach(c; arr) {
      if((cast(A)c) !is null) {
      }
   }
   // ...
}

int f(T)(in A[int] arr)
if(cast(T) !is null) // run time this is ok but not at compile 
time
{
   // ...
}
Jan 18 2021
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 18 January 2021 at 18:40:37 UTC, Jack wrote:
 isInstanceOf from std.traits seems to not work with class the 
 way I need to. I'd like to make a template function accepts 
 only class of a specified class type

 class A { }
 class B : A { }
 class C : A { }

 int f(T)(in A[int] arr)
Use if(is(T : A)) the syntax there is similar to the class declaration itself. isInstanceOf is for checking instances of templates rather than classes.
Jan 18 2021
parent reply Jack <jckj33 gmail.com> writes:
On Monday, 18 January 2021 at 19:02:10 UTC, Adam D. Ruppe wrote:
 On Monday, 18 January 2021 at 18:40:37 UTC, Jack wrote:
 isInstanceOf from std.traits seems to not work with class the 
 way I need to. I'd like to make a template function accepts 
 only class of a specified class type

 class A { }
 class B : A { }
 class C : A { }

 int f(T)(in A[int] arr)
Use if(is(T : A)) the syntax there is similar to the class declaration itself.
is that sytax derived from there?
 isInstanceOf is for checking instances of templates rather than 
 classes.
I see Thanks!
Jan 18 2021
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 18 January 2021 at 19:34:52 UTC, Jack wrote:
 is that sytax derived from there?
sort of. it is the type pattern matching "is expression" described here: https://dlang.org/spec/expression.html#IsExpression The is(A:B) thing technically means "if A is implicitly convertible to B" which of course works for base classes and interfaces well but that's not all it does, like is(int : long) passes too. I just remember the syntax thanks to its similarity to type declarations.
Jan 18 2021
parent Jack <jckj33 gmail.com> writes:
On Monday, 18 January 2021 at 19:54:04 UTC, Adam D. Ruppe wrote:
 On Monday, 18 January 2021 at 19:34:52 UTC, Jack wrote:
 is that sytax derived from there?
sort of. it is the type pattern matching "is expression" described here: https://dlang.org/spec/expression.html#IsExpression
I read std.traits and some related stuff but missed reading is expression doc lol
 The is(A:B) thing technically means "if A is implicitly 
 convertible to B" which of course works for base classes and 
 interfaces well but that's not all it does, like is(int : long) 
 passes too.
the equivalent in D, which would be pretty much same thing
 I just remember the syntax thanks to its similarity to type 
 declarations.
good way to remember too
Jan 18 2021