Thread overview | ||||||
---|---|---|---|---|---|---|
|
July 07, 2011 Any way to get the name of a function? | ||||
---|---|---|---|---|
| ||||
void foo(){}; void bar(){}; void main() { auto funcs = [&foo, &bar]; } I'm using this in a foreach loop and invoking each function with some predefined arguments. But I'd also like to extract the name of each function because each function does some image processing and then I save that image to disk. Basically I want to do: foreach (func; funcs) { func(arguments..); writeToFile(func.stringof); } Obviously I can't use arrays since they don't hold any name information, they just store the pointers. Can I use tuples somehow? The functions are predefined, I just need to iterate through all of them and call them and extract their name. |
July 07, 2011 Re: Any way to get the name of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Maybe something like TypeTuple!(foo ,bar) will work. "Andrej Mitrovic" <andrej.mitrovich@gmail.com> wrote in message news:mailman.1452.1310006894.14074.digitalmars-d-learn@puremagic.com... > void foo(){}; > void bar(){}; > > void main() > { > auto funcs = [&foo, &bar]; > } > > I'm using this in a foreach loop and invoking each function with some predefined arguments. But I'd also like to extract the name of each function because each function does some image processing and then I save that image to disk. > > Basically I want to do: > > foreach (func; funcs) > { > func(arguments..); > writeToFile(func.stringof); > } > > Obviously I can't use arrays since they don't hold any name information, they just store the pointers. > > Can I use tuples somehow? The functions are predefined, I just need to iterate through all of them and call them and extract their name. |
July 07, 2011 Re: Any way to get the name of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 2011-07-07 04:47, Andrej Mitrovic wrote: > void foo(){}; > void bar(){}; > > void main() > { > auto funcs = [&foo,&bar]; > } > > I'm using this in a foreach loop and invoking each function with some > predefined arguments. But I'd also like to extract the name of each > function because each function does some image processing and then I > save that image to disk. > > Basically I want to do: > > foreach (func; funcs) > { > func(arguments..); > writeToFile(func.stringof); > } > > Obviously I can't use arrays since they don't hold any name > information, they just store the pointers. > > Can I use tuples somehow? The functions are predefined, I just need to > iterate through all of them and call them and extract their name. template functionNameOf (alias func) { enum functionNameOf = (&func).stringof[2 .. $]; } -- /Jacob Carlborg |
July 07, 2011 Re: Any way to get the name of a function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 07/06/2011 08:47 PM, Andrej Mitrovic wrote:
> void foo(){};
> void bar(){};
>
> void main()
> {
> auto funcs = [&foo,&bar];
> }
>
> I'm using this in a foreach loop and invoking each function with some
> predefined arguments. But I'd also like to extract the name of each
> function because each function does some image processing and then I
> save that image to disk.
>
> Basically I want to do:
>
> foreach (func; funcs)
> {
> func(arguments..);
> writeToFile(func.stringof);
> }
>
> Obviously I can't use arrays since they don't hold any name
> information, they just store the pointers.
>
> Can I use tuples somehow? The functions are predefined, I just need to
> iterate through all of them and call them and extract their name.
I assume the function signatures are all the same? Then you can do an associative array:
import std.stdio;
int func1(int a) {return a + 1;}
int func2(int a) {return a + 2;}
int func3(int a) {return a + 3;}
void main()
{
int function (int)[string] funcs = ["func1":&func1, \
"func2":&func2, "func3":&func3];
foreach(name, func; funcs)
writef("%s %d\n", name, func(1));
}
Perhaps a mixin would be helpful to limit cut-and-paste issues. If they are methods that are part of a class, you would need to declare them as "int delegate (int)[string] funcs".
|
Copyright © 1999-2021 by the D Language Foundation