playing around with D

Walter Bright newshound at digitalmars.com
Mon Mar 5 11:32:45 PST 2007


Carsten Scharfenberg wrote:
> I just downloaded D and now I'm playing around a bit. My oppinion about D is more or the same as every body else's: D is great - but why does this or that feature not work that am I used to from C++... I think that this is just normal for a new langue :-).
> Anyway I have a couple of questions that arose when I had a look into std.bind. I'm using  dmd v1.007 and gdc v0.22 on Linux.
> 1.

This is a compiler bug, I'll fix it.

> 2.
> 
> The second point seems to be a compiler bug - but correct me if I'm wrong:
> 
> template isTupleContainer( T )
> {
>     static if( is( typeof( T.tuple ) ) )
>     {
>         static if( is( T == TupleContainer!( T.tuple ) ) )
>             static const bool isTupleContainer = true;
>         else
>             static const bool isTupleContainer = false;
>     }
>     else
>         static const bool isTupleContainer = false;
> }
> 
> template DoSomething( T )
> {
>     alias T.tuple  OrigTuple;
>     
>     alias int Result;
> }
> 
> template AssertTest( T )
> {
>     static assert( isTupleContainer!( T ) );
>     
>     alias DoSomething!( T ).Result    AssertTest;
> /*      alias int AssertTest;*/
> }
> 
> void main()
> {
>     /*
>      * this is perfectly fine and prints false
>      */
>     writefln( typeid( isTupleContainer!( int ) ) );

Should be:
	writefln( isTupleContainer!(int) );

>     
>     /*
>      * this should trigger the assert in AssertTest - but it doesn't,
>      * a compiler error is emitted instead.
>      */
>     writefln( typeid( AssertTest!( int ) ) );
> }
> 
> 
> Error Message:
> test.d(xxx): Error: no property 'tuple' for type 'int'
> test.d(xxx): Error: T.tuple is used as a type
> test.d(xxx): template instance test.DoSomething!(int) error instantiating
> 
> 
> isTupleContainer checks, of course, if T is a TupleContainer. This works fine in
> the main function - but in AssertTest its result seems always to be true
> so that the assert is never triggered.

The problem is you are expecting that the static assert is checked 
before the alias in AssertTest. Multiple semantic passes are done over 
the declarations, and static assert's aren't checked until later. To 
ensure an order, use static if, such as:

template AssertTest( T )
{
     static if( isTupleContainer!( T ) )
         alias DoSomething!( T ).Result    AssertTest;
     else
	static assert(0);
}



More information about the Digitalmars-d mailing list