Here's another one that might work, and be less error-prone:

mixin template Self() {
  auto self = __traits(identifier, __traits(parent, {}));
}

void test() {
  mixin Self;
  writeln(self);
}

On 23/09/2012 09:58, Rob T wrote:
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.


Thanks, for comming up with this clever solution, it's the best one so
far, and I will likely be using it.

The real problem of course is that there is no simple or obvious
solution to what is a very basic requirment - to inspect information
about a calling function, and/or execute it recursively without having
to re-specify the function explicitly. Don't get me wrong, I am very
much impressed with D, but the reflection component in D should be made
a real part of the language, and really ought to be made more
generalized (elegant).

It is interesting that both classes and structs are able to refer to
themselves through "this", so that's a hint that there may be some more
use to the idea of self-referencing elsewhere. For example, the ctors
and dtors refer back to "this", hinting that functions should be able to
do the same thing for recursive calls.

--rt


Reply via email to