www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template mixin identifier as template alias parameter

reply "Jack Applegame" <japplegame gmail.com> writes:
I'm trying creating template for intrusive double-linked list:

mixin template node() {
   static if(is(this == struct))
     alias typeof(this)* E;
   else
     alias typeof(this) E;
   E prev, next;
}

struct list(alias N) {
   N.E head;
   N.E tail;
}

class A {
   mixin node;
}

list!A l;

All works great.
If it's need to store one object in two different lists I plan 
something like this:

class A {
   mixin node L1;
   mixin node L2;
}

list!(A.L1) l1;
list!(A.L2) l2;

But compiler doesn't compile that whith error:
"this is not in a class or struct scope|"
in line with
"alias typeof(this) E;"
Nov 11 2012
parent reply Jacob Carlborg <doob me.com> writes:
On 2012-11-11 10:04, Jack Applegame wrote:
 I'm trying creating template for intrusive double-linked list:

 mixin template node() {
    static if(is(this == struct))
      alias typeof(this)* E;
    else
      alias typeof(this) E;
    E prev, next;
 }
 class A {
    mixin node L1;
    mixin node L2;
 }
This won't work. The "node" template doesn't evaluate to a type and you wouldn't use it as a mixin if it did.
 But compiler doesn't compile that whith error:
 "this is not in a class or struct scope|"
 in line with
 "alias typeof(this) E;"
-- /Jacob Carlborg
Nov 11 2012
next sibling parent "Jack Applegame" <japplegame gmail.com> writes:
On Sunday, 11 November 2012 at 10:36:42 UTC, Jacob Carlborg wrote:
 This won't work. The "node" template doesn't evaluate to a type 
 and you wouldn't use it as a mixin if it did.
But this is works! Why? mixin template node() { alias int E; E prev, next; } struct list(alias N) { N.E head; N.E tail; } class A { mixin node L1; mixin node L2; } list!(A.L1) l1; list!(A.L2) l2;
Nov 11 2012
prev sibling parent "David Nadlinger" <see klickverbot.at> writes:
On Sunday, 11 November 2012 at 10:36:42 UTC, Jacob Carlborg wrote:
 This won't work. The "node" template doesn't evaluate to a type 
 and you wouldn't use it as a mixin if it did.
You _can_ actually use an identifier for mixin templates, it is used to disambiguate in case of name collisions. As far as the original question goes, I don't see a reason why this shouldn't work; you might want to add a bug report at http://d.puremagic.com/issues. David
Nov 11 2012