www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a more elegant way of testing T for multiple types?

reply Gary Willoughby <dev nomad.so> writes:
Here I'm testing T is either a class or interface:

void foo(T)(T bar) if (is(T == class) || is(T == interface))
{
     ...
}

Is there a more elegant way of testing T for multiple types? 
Because it doesn't scale well if I need to add more.

I would love to use something like this:

void foo(T)(T bar) if (T in [class, interface])
{
     ...
}

Is something like this currently possible using type tuples?
Jun 18 2016
parent ag0aep6g <anonymous example.com> writes:
On 06/18/2016 04:37 PM, Gary Willoughby wrote:
 Here I'm testing T is either a class or interface:

 void foo(T)(T bar) if (is(T == class) || is(T == interface))
 {
      ...
 }

 Is there a more elegant way of testing T for multiple types? Because it
 doesn't scale well if I need to add more.

 I would love to use something like this:

 void foo(T)(T bar) if (T in [class, interface])
 {
      ...
 }

 Is something like this currently possible using type tuples?
There's std.meta.staticIndexOf [1]. Use it like so: void foo(T)(T bar) if (staticIndexOf!(T, int, float) >= 0) { ... } Doesn't work with `class` or `interface`, though, because those are not types. [1] https://dlang.org/phobos/std_meta.html#.staticIndexOf
Jun 18 2016