6
votes

Given a function declaration in D, is it possible to introspect at compile time the string representation of any function parameter names, for use in say automatic function reflection. E.g.

void foo(int a, double b, string c) { }
register_function!(foo)()

Can register_function extract "a","b","c" at compile time in a similar way that __traits(allMembers,someClass) can for a class?

2
What do you need it for? I don't see how your example requires it. - BCS

2 Answers

4
votes

You can use std.traits.ParameterTypeTuple!() to get the types of the parameters, but I'm not aware of any way to get their names. std.traits is continuously being improved, however, so that my get added. Odds are that is just that no one working on it has thought of that particular need, so they haven't added it yet. I would suggest creating an enhancement request for it, and there's a good chance that they'll add it.

3
votes

I think one of the uses of stringof gives the names. You can parse them out with a bit of work. OTOH stringof is ill-defined so this would be a bit brittle.