digitalmars.D.learn - Any way to get the name of a function?
- Andrej Mitrovic (20/20) Jul 06 2011 void foo(){};
- Daniel Murphy (3/23) Jul 06 2011 Maybe something like TypeTuple!(foo ,bar) will work.
- Jacob Carlborg (7/27) Jul 06 2011 template functionNameOf (alias func)
- Kai Meyer (17/37) Jul 07 2011 I assume the function signatures are all the same? Then you can do an
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.
Jul 06 2011
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.
Jul 06 2011
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
Jul 06 2011
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".
Jul 07 2011