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