Function name

Jacob Carlborg doob at me.com
Fri Apr 23 05:02:57 PDT 2010


On 4/23/10 02:14, Adam D. Ruppe wrote:
> 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?

Yes we still need classinfo, if you you store an object in a base class 
reference classinfo will give you the runtime type (the subclass) and 
__traits will give you the compile time type (the base class).



More information about the Digitalmars-d mailing list