template+alias as an object's "namespace"

Sebastian Graf SebastianGraf at t-online.de
Tue May 14 06:31:03 PDT 2013


On Thursday, 19 April 2012 at 22:24:52 UTC, F i L wrote:
> snip

Sorry to dig this up again, but I'm looking for something to 
provide stubs for a compile time interface in a struct:

mixin template Stubs() {
     template __Stubs__() {
         void sayHi() { writeln("hi"); }
     }
     alias __Stubs__!() this;
}

struct Foo {
     mixin Stubs;
}

void main() {
     Foo().sayHi();
}

This is so that I am still able to override sayHi() at compile 
time with my own implementation in Foo.
I would love to do that, but it won't compile right now. If I 
alias to something else than this, it works though.
I could alias-this with an inner struct, i.e. when I change the 
template mixin to

mixin template Stubs() {
     struct __Stubs__ {
         void sayHi() { writeln("hi"); }
     }
     private __Stubs__ __s;
     alias __s this;
}

But this means I have to sacrifice some bytes in Foo to store the 
super struct, noticable as you add fields to Foo. This is not too 
bad, but since we have templates and mixins and stuff, is there 
maybe a way I haven't thought about?




More information about the Digitalmars-d mailing list