Getting the name of current module

Philippe Sigaud philippe.sigaud at gmail.com
Fri Aug 27 13:59:51 PDT 2010


On Fri, Aug 27, 2010 at 01:09, Andrej Mitrovic
<andrej.mitrovich at gmail.com>wrote:

> import Python
> dir()
>
> etc.. :)
>
> I want more reflection as well, it's plenty of fun.
>
>
I will use my mangling/demangling trick, and do it at CT using

http://digitalmars.com/d/2.0/abi.html

I wonder if one can get the name of function while being inside ?
Ah, yes, you can:

module main;
import std.stdio;
import std.traits;
import std.demangle;

void foo(int i) {
    double test;
    writeln(i.mangleof);
    writeln(test.mangleof);
    writeln(demangle(i.mangleof));
    writeln(demangle(test.mangleof));
    writeln(mangledName!i);
    writeln(mangledName!test);
    writeln(demangle(mangledName!i));
    writeln(demangle(mangledName!test));
}

void main()
{
    foo(1);
    int i;
    double test;
    writeln(i.mangleof);
    writeln(test.mangleof);
    writeln(demangle(i.mangleof));
    writeln(demangle(test.mangleof));
    writeln(mangledName!i);
    writeln(mangledName!test);
    writeln(demangle(mangledName!i));
    writeln(demangle(mangledName!test));
}

Extracting 'main' or 'foo' from the right place in the mangle shouldn't be
too difficult. So doing a template that alias itself to the name of the
current function or module is doable. std.demangle use a try catch statement
-> it's not usable at compile-time.
Hmm, that pretty interesting! I was wondering how to create generic
recursive functions from strings like "a + self(b)". Now, I can get the
function name, replace self with it and mix the code in the right place.


The built-in .mangleof property is a bit strange. Does it work correctly?


Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100827/04eb0227/attachment.html>


More information about the Digitalmars-d mailing list