D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2909 - Get the name of function and its parameters
Summary: Get the name of function and its parameters
Status: RESOLVED DUPLICATE of issue 5140
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-29 01:41 UTC by Georg Wrede
Modified: 2015-06-09 05:14 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Georg Wrede 2009-04-29 01:41:32 UTC
Phobos should provide an easy and documented way to get the name of the function and its parameters.

The function name and its parameters are unneeded in "straight" code, but with mixins and templates it is important. And it's essential for debugging them and their usage.

Here's some code from D.learn, as a starting point:


// 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.
// 
// Don't you love it?
// "Most C++ template features are discovered."  So are D's.

// Thanks.
// 
// I found that this: 

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.
Comment 1 Andrej Mitrovic 2013-01-11 19:39:54 UTC

*** This issue has been marked as a duplicate of issue 5140 ***