type set

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 28 05:32:44 PST 2011


On Mon, 28 Feb 2011 08:22:58 -0500, spir <denis.spir at gmail.com> wrote:

> Hello,
>
> I have a template condition that looks like this:
>
>      T check (T) () if (
>              is(T == DLogical) ||
>              is(T == DNumber) ||
>              is(T == DText) ||
>              is(T == DList) ||
>              is(T == DUnit)
>          ) {
>          ...
>      }
>
> Is there a way to "factor out" such an expression using a kind of type  
> set? If only for cleaning the code; but also because such a set may get  
> long.
>
>      T check (T) () if (is (T in validTypeSet)) {
>          ...
>      }

This should probably work:

template isOneOf(X, T...)
{
     static if(!T.length)
        enum bool isOneOf = false;
     else static if(is(X == T[0]))
        enum bool isOneOf = true;
     else
        enum bool isOneOf = isOneOf!(X, T[1..$]);
}

T check(T) () if(isOneOf!(T, DLogical, DNumber, DText, TList, DUnit))
{
    ...
}

Not sure if this exists in std.traits or not, but that's where I'd look.

-Steve


More information about the Digitalmars-d-learn mailing list