On Thu, Apr 22, 2010 at 07:48:31PM -0400, bearophile wrote:
> The third solution is what I'd like to be able to write.

How about this?

===

import std.stdio;
import std.traits;

int add(int a, int b) { return a+b; }
int mul(int a, int b) { return a*b; }

auto funcwithName(T...)() {
        typeof(&T[0])[string] ret;
        foreach(t; T)
                ret[__traits(identifier, t)] = &t;
        return ret;
}

void main() {
        foreach(name, func; funcwithName!(add, mul)) {
                writefln("%s %d", name, func(10,20));
        }
}

===
$ ./funname
mul 200
add 30


All the functions have to be the same type to loop over them like this, and
it might not work with zero argument functions.

> This can inflate the binary a little. This is not too much different from the 
> data classes have, like their name that can be found with classinfo.

Btw, do we still need classinfo now that we have __traits?

-- 
Adam D. Ruppe
http://arsdnet.net

Reply via email to