The is expression
enuhtac
enuhtac_lists at gmx.de
Tue Apr 5 03:21:30 PDT 2011
Am 03.04.2011 16:11, schrieb Philippe Sigaud:
> On Sat, Apr 2, 2011 at 13:05, enuhtac <enuhtac_lists at gmx.de> wrote:
>> This is the type I would like to check for:
>>
>> struct A( T, string s )
>> { ... };
> Hi,
>
> the trick is to use a function do the work for you. Let's define isAnA:
>
> void isAnA(T, string s)( A!(T,s) a) { }
>
> isAnA can only be called (compiled) with your A.
>
> A!(int, "abc") a;
> A!(double, "") b;
>
> isAnA(a); // OK
> isAnA(b); // OK
> isAnA(123); // does not compile.
>
> So you can nest it in a template and check at compile-time if it compiles:
>
> template isMyA(Type)
> {
> static if (__traits(compiles,
> {
> void isAnA(T, string s)(A!(T,s) a) {}
> isAnA(Type.init); // create a value of
> type Type, see if isAnA accepts it.
> }))
> enum bool isMyA = true;
> else
> enum bool isMyA = false;
> }
>
> Note that this is strictly tailored to A's with (T, string s) as
> arguments. You can relax the constraints by adapting the test
> function.
>
> For a more generic way to test for this, you can have a look there:
>
> http://svn.dsource.org/projects/dranges/trunk/dranges/docs/templates.html
> ("isInstanceOf" and "Template ParametersTypeTuple")
> http://svn.dsource.org/projects/dranges/trunk/dranges/docs/typepattern.html
> (look for "isA")
>
>
> Philippe
Hi Philippe,
thanks for your answer. If it is that complicated if I prefer explicit
specialization, I think. But I do not quite understand that it is not
possible to achieve this with a simple "is" expression (without a
function like "isAnA" and using "__traits") as the D language reference
includes a very similiar example based on arrays:
static if (is(int[10] W : W[V], int V))
what is the essential difference to:
static if( is( A!(int, "xxx") T == A!(T, s), string s ) )
?
enuhtac
More information about the Digitalmars-d-learn
mailing list