On 18/09/2012 22:21, Rob T wrote:
On Tuesday, 18 September 2012 at 19:57:05 UTC, Ben Davis wrote:
Wild stab in the dark, but would something like this work:

void myfunction() {
    int dummy;
    auto self = __traits(parent, dummy);
}

though it would be better if something like __traits(enclosing) were
supported.



This hack will generate the enclosing function name

void function x()
{
   int dummy;
   auto funcname = __traits( identifier, __traits( parent, dummy ) );
   writefln( funcname );
}

Obviously the above solution is not reasonable, esp for a language that
attempts to provide good reflection abilities.

I've just made a wrapper that helps - you have to use mixin with it:

string scopeName()
{
    return q{__traits(identifier, __traits(parent, {}))};
}

void main()
{
    writeln(mixin(scopeName()));
}

That should be quite usable, except if the user forgets the mixin() part - unfortunately that potential bug seems to be statically undetectable.

Reply via email to