Thread overview
bug(?) with recursive aliases
Feb 09, 2006
Sean Kelly
Feb 10, 2006
Sean Kelly
Feb 11, 2006
Thomas Kuehne
February 09, 2006
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>
February 10, 2006
I think I'm merely running up against the limitations of 'alias' here. Can 'alias' be used to alias variables?  And if so, what are the restrictions?  The spec isn't of much use here.  For reference, here is a shortened test case that exhibits the problem.  Why can I alias 'i' but not 's.i'?  Is it merely a scope issue?

C:\code\d\bugs>type 145_2.d
void main()
{
    struct S { int i; }
    S   s;
    int i;

    alias i   a1;
    alias s.i a2;
}
C:\code\d\bugs>dmd 145_2.d
145_2.d(8): s.i is used as a type

C:\code\d\bugs>
February 11, 2006
Sean Kelly schrieb am 2006-02-10:
> I think I'm merely running up against the limitations of 'alias' here. Can 'alias' be used to alias variables?  And if so, what are the restrictions?  The spec isn't of much use here.  For reference, here is a shortened test case that exhibits the problem.  Why can I alias 'i' but not 's.i'?  Is it merely a scope issue?
>
> C:\code\d\bugs>type 145_2.d
> void main()
> {
>      struct S { int i; }
>      S   s;
>      int i;
>
>      alias i   a1;
>      alias s.i a2;
> }
> C:\code\d\bugs>dmd 145_2.d
> 145_2.d(8): s.i is used as a type

Added to DStress http://dstress.kuehne.cn/run/a/alias_29_A.d http://dstress.kuehne.cn/run/a/alias_29_B.d

Thomas