Getting the names of all the top-level functions in module
Adam D Ruppe
destructionator at gmail.com
Tue Sep 21 15:07:35 UTC 2021
Here's one with param names:
----
module qwe;
struct FunctionInfo {
string name;
string[] paramNames;
}
enum FunctionInfo[] functionInfo(alias M) = ()
{
FunctionInfo[] res;
foreach(memberName; __traits(derivedMembers, M))
static if (is(typeof(__traits(getMember, M,
memberName)) Params == __parameters)) {
FunctionInfo fi = FunctionInfo(memberName);
foreach(idx, param; Params)
fi.paramNames ~= __traits(identifier,
Params[idx .. idx + 1]);
res ~= fi;
}
return res;
}();
void main() {
import std.stdio;
writeln(functionInfo!qwe);
}
void foo(int a, int b){}
----
Of course the weird thing here is the need to slice the params
tuple to get the idenfitier. I had to borrow that trick from
Phobos but now that I know it it is very useful.
Otherwise the code is still fairly simple. Only hte one is
expression since if it doesn't have params it doesn't trigger the
condition and if all functions have params (even if it is an
empty set)
More information about the Digitalmars-d
mailing list