digitalmars.D.learn - Mixin Global Variables
- Brian White (11/11) Mar 17 2008 I want to create a module/global variable as the result of a template
- Jarrett Billingsley (24/29) Mar 17 2008 It should be.
- Brian White (3/13) Mar 19 2008 That makes sense and I got it to work just fine.
I want to create a module/global variable as the result of a template mixin. However, this variable needs some sort of "static this" initialization. Is this possible with a single mixin? (i.e. declare the variable AND create a static initializer for it) In C++, I'd do this with a global static instance of a class with a constructor that initializes the instance. I can't seem to do this with D because only classes have constructors and they're always created via "new". Thanks! -- Brian
Mar 17 2008
"Brian White" <bcwhite pobox.com> wrote in message news:frlpm6$3km$1 digitalmars.com...I want to create a module/global variable as the result of a template mixin. However, this variable needs some sort of "static this" initialization. Is this possible with a single mixin? (i.e. declare the variable AND create a static initializer for it)It should be. If you're using a template mixin: template foo(T) { T bar; static this() { bar = something(); } } mixin foo!(int); If you're using a string mixin: template foo(T) { const char[] foo = T.stringof ~ ` bar; static this() { bar = something(); }`; } mixin(foo!(int));
Mar 17 2008
template foo(T) { T bar; static this() { bar = something(); } } mixin foo!(int);That makes sense and I got it to work just fine. Thanks! -- Brian
Mar 19 2008