www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin Global Variables

reply Brian White <bcwhite pobox.com> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent Brian White <bcwhite pobox.com> writes:
 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