| |
| Posted by Sean Kelly | PermalinkReply |
|
Sean Kelly
| I'm not sure whether this is a bug, but I haven't been able to find a syntax that works. Notice that t.val!(0) is fine, so the problem is expanding "alias tail.val!(n-1) val":
C:\code\d>type test.d
void main()
{
auto t = new Tuple!(int,Tuple!(int))();
t.val!(0) = 1;
t.val!(1) = 2;
}
struct Empty
{
}
class Tuple( HeadType, TailType = Empty )
{
alias HeadType Type;
HeadType head;
TailType tail;
template val( int n )
{
static if( n == 0 )
alias head val;
else static if( is( typeof(tail) == Empty ) )
alias tail val;
else
alias tail.val!(n-1) val;
}
}
C:\code\d>dmd test
test.d(27): template identifier val is not a member of tail
test.d(6): template instance test.Tuple!(int,Tuple).Tuple.val!(1) error instantiating
test.d(6): cannot implicitly convert expression (2) of type int to test.Tuple!(int).Tuple
C:\code\d>
I also tried partial specialization, thinking the problem might be that Empty doesn't define a val template member:
C:\code\d>type test.d
void main()
{
auto t = new Tuple!(int,Tuple!(int))();
t.val!(0) = 1;
t.val!(1) = 2;
}
struct Empty
{
}
class Tuple( HeadType, TailType = Empty )
{
alias HeadType Type;
HeadType head;
TailType tail;
template val( int n, T : Empty = TailType )
{
static if( n == 0 )
alias head val;
else
alias tail val;
}
template val( int n, T = TailType )
{
static if( n == 0 )
alias head val;
else
alias tail.val!(n-1,TailType) val;
}
}
C:\code\d>dmd test.d
test.d(34): template identifier val is not a member of tail
test.d(6): template instance test.Tuple!(int,Tuple).Tuple.val!(1) error instantiating
test.d(6): cannot implicitly convert expression (2) of type int to test.Tuple!(int).Tuple
C:\code\d>
|