scope keyword within template mixins
KlausO
oberhofer at users.sf.net
Mon Jun 28 07:14:57 PDT 2010
Hi D users,
I created/tested a little logging class under DMD 1.062:
//-------------------------------------------------------------------------
//
// helper class for debugging/logging
//
import std.string;
import std.stdio;
scope class LogEnter
{
static int mIndent;
static char[] doindent()
{
return "\n" ~ repeat(" ", mIndent);
}
this(char[] text)
{
mText = text;
writef(doindent() ~ "<" ~ mText ~ ">");
mIndent += 2;
}
~this()
{
mIndent -= 2;
writef(doindent() ~ "</" ~ mText ~ ">");
}
char[] mText;
}
//-------------------------------------------------------------------------
//
// logging with scope keyword
//
void C()
{
scope auto log = new LogEnter("C");
D();
}
void D()
{
scope auto log = new LogEnter("D");
}
void main()
{
// C();
}
//-------------------------------------------------------------------------
Together with the scope keyword this works as expected. The output looks
like:
<C>
<D>
</D>
</C>
After that I tried to move the LogEnter instantiation into a template
mixin, as follows:
//-------------------------------------------------------------------------
//
// logging via template mixin
//
template Log(char[] msg)
{
scope auto log = new LogEnter(msg);
}
void A()
{
mixin Log!("A");
B();
}
void B()
{
mixin Log!("B");
}
void main()
{
A();
}
//-------------------------------------------------------------------------
I get the following output:
<A>
<B>
IMO the template mixin variant should behave the same. Did I miss
something or is this a bug ?
I already searched bugzilla for similar bugs, but I found none.
Thanks in advance for your help
KlausO
More information about the Digitalmars-d-learn
mailing list