reference to 'self' inside a function

Ben Davis entheh at cantab.net
Tue Sep 18 14:52:17 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.
>
> What would work considerably better is something ike this
>
> void function x()
> {
>     writeln(self.symbolname);
> }

I agree - except I wouldn't reserve 'self' as a keyword. I'd probably 
extend the existing __traits mechanism, avoiding the need for the 
dummy/parent hack. Possibly also create a shortcut for nested __traits?

It's not down to me though - I just wanted to give you something to work 
with so you can get your project working.

D has a bug tracker where you could post the feature request:

http://d.puremagic.com/issues/enter_bug.cgi?product=D

Good luck :)


More information about the Digitalmars-d mailing list