typeof(this) at class scope?

Bill Baxter dnewsgroup at billbaxter.com
Sun Jul 8 14:11:30 PDT 2007

Is this supposed to work?
import std.stdio : writefln;

class Foo {
     alias typeof(this) this_type; // <<--- seems odd but works

     this(int val) { value = val; }

     int value;

void main()
     auto foo = new Foo.this_type(10);


It does work currently, but I just want to make sure this is intentional 
and isn't going to go away.  It seems odd that it works because I 
thought 'this' didn't really exist outside method bodies.

The actual use case is in a mixin that is used to implement the bulk of 
of a couple of different variations of the same class.  But the mixin 
needs to know the type of the thing it's implementing for writing 
various methods.  If typeof(this) isn't kosher, then I could pass in the 
type as a template parameter explicitly but that's not as nice.


More information about the Digitalmars-d-learn mailing list