reference to 'self' inside a function

Ben Davis entheh at cantab.net
Tue Sep 18 13:24:07 PDT 2012


Or indeed

auto self = __traits(parent, self);

Grin :)

On 18/09/2012 20:57, 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.
>
> On 18/09/2012 20:22, Rob T wrote:
>> I was just about to make this proposal myself before searching to see if
>> it had already been discussed, so here I am.
>>
>> The requirement for self-referencing is a lot more profound than
>> providing only a means to self-reference a calling function (runtime or
>> during compile time).
>>
>> I recently decided to drop C++ and commit to using D for a production
>> system (yeah I'm taking a risk), and almost immediately the first
>> problem I encounter is this insanely frustrating inability:
>>
>> For logging errors in my code, I want to log the calling function name
>> along with other applicable information. I can do a lot with D, but I
>> cannot get access to the calling function name in a reasonable way (all
>> solutions I have found so far are IMO unreasonable).
>>
>> I can easily log class names from a member function, eg
>> typeof(this).stringof
>>
>> So why can't I do a similar thing with member functions? or any other
>> type that has internal member code? or more profoundly, any type in
>> general eg entity.this?
>>
>> (I know 'this' is a special key word, I re-use it to illustrate the
>> point)
>>
>> I recall how it was nice to hear that D did away with the need to prefix
>> class names onto cstors and dstors (that was required for more than one
>> reason), so here is the opportunity to do the same thing with functions,
>> such that recursive calling does not require explicityly re-naming the
>> same function over again.
>>
>> I also realize that there's a need to introduce better compile time
>> reflection and runtime reflection, so here is an opportunity to unify a
>> few things that directly apply to both of these efforts.
>>
>> Self-referencing should be generalized and applicable to all entities,
>> not just classes and structs. It could make templates easier to write,
>> it directly applies to improved compile and runtime reflection, and it
>> elegantly solves real world proplems, such as my simple error logger
>> requirement.
>>
>> --rt
>>
>



More information about the Digitalmars-d mailing list