www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - The is expression

Hello,

the "is" expression is a great feature of D - but its use is not very
intuitive, at least for me.
I'm trying to write a template that figures out if the template
parameter is of a given type.
This is the type I would like to check for:

struct A( T, string s )
{ ... };

One possibility to accomplish this check is explicit template
specialization:

template isA( T )
{
    enum bool isA = false;
};

template isA( T : A!( U, s ), U, string s )
{
    enum bool isA = true;
};

This more or less the C++ approach. But in D this could also be done
with static if and the "is" expression. As I understand "is" it should
be done like this:

template isA( T )
{
    static if( is( T U == A!( U, s ), string s ) )
        enum bool isA = true;
    else
        enum bool isA = false;
};

But this does not work. So what am I doing wrong?

Regards,
enuhtac
Apr 01 2011