February 23, 2014 loop over all parameters of all overloads of all members | ||||
---|---|---|---|---|
| ||||
The following code craps out on the inner for each foreach (am; __traits(derivedMembers, B)) foreach (m; [__traits(getOverloads, B, am)]) { } So how can I loop over all the parameters of all the overloads of all the members of a class? Create some weird mapping tuple? I guess we are not going to get a static foreach any time soon? |
February 23, 2014 Re: loop over all parameters of all overloads of all members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frustrated | On Sunday, 23 February 2014 at 21:08:48 UTC, Frustrated wrote: > The following code craps out on the inner for each > > foreach (am; __traits(derivedMembers, B)) > foreach (m; [__traits(getOverloads, B, am)]) > { > > } > > So how can I loop over all the parameters of all the overloads of > all the members of a class? > > Create some weird mapping tuple? Here's an example of how to loop over all paremters of all the methods in a class T and print the functions with arguments. import std.typetuple, std.traits; class B { void b() { } void b(int i) { } void b(int i, long l) { } } string signatureString(alias func)() { alias pt = ParameterTypeTuple!func; alias pi = ParameterIdentifierTuple!func; alias rt = ReturnType!func; enum name = __traits(identifier, func); enum stat = __traits(isStaticFunction, func) ? "static " : ""; string s = stat ~ rt.stringof ~ " " ~ name ~ "("; foreach(i, dummy; pt) { s ~= pt[i].stringof ~ " " ~ pi[i]; static if(i != pt.length - 1) s ~= ","; } s ~= ")"; return s; } void ctPrintClassFunctions(T)() { foreach(member; __traits(allMembers, T)) { static if(__traits(compiles, __traits(getOverloads, T.init, member))) { alias Overloads = TypeTuple!(__traits(getOverloads, T.init, member)); foreach(overload; Overloads) { pragma(msg, signatureString!overload); } } } } void main() { ctPrintClassFunctions!B; } > I guess we are not going to get a static foreach any time soon? Every foreach in the example is a "static" foreach in the sense that it unrolls the loops and evaluates them at compiletime. |
Copyright © 1999-2021 by the D Language Foundation