Get the name of a function and the parameters?
Jacob Carlborg
doob at me.com
Mon Apr 27 03:56:18 PDT 2009
Jarrett Billingsley wrote:
> On Sun, Apr 26, 2009 at 12:23 PM, Jacob Carlborg <doob at me.com> wrote:
>> Is it possible to get the name of a function and the names of the function
>> parameters?
>
> Name of a function? Yes.
>
> public template NameOfFunc(alias f)
> {
> version(LDC)
> const char[] NameOfFunc = (&f).stringof[1 .. $];
> else
> const char[] NameOfFunc = (&f).stringof[2 .. $];
> }
>
> debug
> {
> private void _foo_(){}
> static assert(NameOfFunc!(_foo_) == "_foo_", "Oh noes, NameOfFunc
> needs to be updated.");
> }
>
> It has to be used at compile time with an alias of course, but it works.
>
> Name of params? No, not that I've found. Sometimes the compiler will
> give parameter names of functions declared with tuple parameters in
> error messages; I wonder if that could be abused.
>
> Don't you love it? "Most C++ template features are discovered." So are D's.
Thanks.
I found that this:
void foo (int x, int y)
{
}
void main ()
{
pragma(msg, typeof(&foo).stringof);
}
gave this result:
void function(int x, int y)
So now I just have to get the names out of there.
More information about the Digitalmars-d-learn
mailing list