Thread overview | |||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 26, 2009 Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Is it possible to get the name of a function and the names of the function parameters? |
April 26, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg |
Jacob Carlborg wrote:
> Is it possible to get the name of a function and the names of the function parameters?
I don't believe so, no.
-- Daniel
|
April 27, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Sun, Apr 26, 2009 at 12:23 PM, Jacob Carlborg <doob@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.
|
April 27, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep |
Daniel Keep wrote:
>
> Jacob Carlborg wrote:
>> Is it possible to get the name of a function and the names of the function parameters?
>
> I don't believe so, no.
>
> -- Daniel
I should perhaps qualify, in light of Jarrett's response, that I thought you meant from inside the function ala __FUNCTION__ or somesuch.
-- Daniel
|
April 27, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> On Sun, Apr 26, 2009 at 12:23 PM, Jacob Carlborg <doob@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.
You can get the name of a local variable from within a scope that variable is active at compile-time. Pass it as an alias parameter and then start demangling in there; it's usually towards the end.
|
April 27, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> On Sun, Apr 26, 2009 at 12:23 PM, Jacob Carlborg <doob@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.
|
April 27, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep | Daniel Keep wrote:
>
> Daniel Keep wrote:
>> Jacob Carlborg wrote:
>>> Is it possible to get the name of a function and the names of the
>>> function parameters?
>> I don't believe so, no.
>>
>> -- Daniel
>
> I should perhaps qualify, in light of Jarrett's response, that I thought
> you meant from inside the function ala __FUNCTION__ or somesuch.
>
> -- Daniel
Jarrett's solution was exactly what I needed.
|
April 27, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Mon, Apr 27, 2009 at 6:56 AM, Jacob Carlborg <doob@me.com> wrote:
> 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.
Interesting! I wonder if that changed recently.
|
April 28, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote:
> On Mon, Apr 27, 2009 at 6:56 AM, Jacob Carlborg <doob@me.com> wrote:
>
>> 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.
>
> Interesting! I wonder if that changed recently.
I don't think so, I'm using gdc.
|
April 28, 2009 Re: Get the name of a function and the parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote: > On Sun, Apr 26, 2009 at 12:23 PM, Jacob Carlborg <doob@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. |
Copyright © 1999-2021 by the D Language Foundation