Am 02.04.2011 04:00, schrieb Caligo:
On Fri, Apr 1, 2011 at 5:14 PM, enuhtac <enuhtac_lists@gmx.de> wrote:
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


I'm new too, but I think it should be like this:

template isA( T ){

  enum bool isA = is(T : A)
}

if the name of enum is same as the template then you could use it as such:

if( isA( T ) ){ }

instead of

if( isA( T ).isA ){ }

Also note that : allows implicit conversion, while == requires the
types to be exactly the same.
Your right. In your example it is possible to circumvent the "static if" construct. I was not aware of this, so thanks for the hint.
But in my case this does not work as I'm using the following "is" form:

is ( Type Identifier == TypeSpecialization , TemplateParameterList )

Obviously "is" forms that include an "Identifier" can only be used in "static if" constructs. Actually I could do without the "Identifier" but I need the "TemplateParameterList". But there is no "is" form where you get a "TemplateParameterList" without an "Identifier".
Actually that would be nice as this would look like the explicit specialization:
template isA( T )
{
   static if( is( T == A!( U, s ), U, string s ) )
       enum bool isA = true;
   else
       enum bool isA = false;
};
Or with your simplification:
template isA( T )
{
   enum bool isA = is( T == A!( U, s ), U, string s );
};

But this is not possible as the "Identifier" is missing.

So still I do not know what I'm doing wrong in my original code.