Question about template mixins as nested functions.

Derek Parnell derek at nomail.afraid.org
Thu Feb 15 21:52:46 PST 2007


On Fri, 16 Feb 2007 00:31:07 -0500, Gerald Stocker wrote:

> Hello,
> 
> I have a question about using a template mixin to add a nested function 
> to a class method...

I have trouble with mixin templates too. I believe the effect you see is
because the mixin introduces its own scope and you have to qualify
reference that are outside the mixin.

However, the new mixin(Statement) might be useful to you ...

-------------
import std.stdio;
class SomeClass
{
     int memberVar;
     void doSomething()
     {
        int localVar;
        mixin(helperFunction);
        helperFunction();
     }
}

const char[] helperFunction = "
     void helperFunction()
     {
         localVar++;
         memberVar++;
     }
    "; 
void main()
{
    SomeClass x = new SomeClass();
    x.doSomething();
    writefln("memberVar = %s", x.memberVar);
    x.doSomething();
    writefln("memberVar = %s", x.memberVar);
}
--------------



-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
16/02/2007 4:49:50 PM


More information about the Digitalmars-d-learn mailing list