reference to 'self' inside a function
Nick Treleaven
ntrel-public at yahoo.co.uk
Sat Sep 22 09:31:11 PDT 2012
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.
More information about the Digitalmars-d
mailing list