Get the name of a function and the parameters?

grauzone none at example.net
Tue Apr 28 12:07:19 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.

I'd like to pass several functions at once. Is there a way to make this 
variadic? The obvious approach (writing "NameOfFunc(alias f...)") fails 
with a syntax error.

> 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.


More information about the Digitalmars-d-learn mailing list