Thread overview
Mixin Global Variables
Mar 17, 2008
Brian White
Mar 19, 2008
Brian White
March 17, 2008
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
March 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));



March 19, 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