6

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?

1
  • What do you need it for? I don't see how your example requires it.
    – BCS
    Commented Nov 3, 2010 at 5:05

2 Answers 2

4

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
  • Please do place an enhancement request! This would be very useful for creating binding libraries like Boost.Python or luabind.
    – deft_code
    Commented Nov 3, 2010 at 16:00
  • 1
    Since, I see no sign of John creating an enhancement request for this, I created one: d.puremagic.com/issues/show_bug.cgi?id=5175 Commented Nov 5, 2010 at 22:14
  • Thanks very much for putting forward this request for me.
    – John
    Commented Nov 6, 2010 at 21:01
3

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.

4
  • I tried to use stringof on a function, and it just complains about it not being callable with the given argument type. Using stringof on a function call results in a string of the function call with its arguments, not the names of the parameters. So, if there is a way to get stringof to give you the names of the function parameters, I don't know what it is. Commented Nov 3, 2010 at 4:23
  • 2
    @Jonathan: you have to apply stringof to the type. I.e., if you define void register_function(T)(T f) { auto a = T.stringof; }, then a call register_function(&foo); gives a == "void function(int a, double b, string c)". BCS suggests to parse this string (which definitely is a bit brittle, indeed).
    – stephan
    Commented Nov 3, 2010 at 7:07
  • Thanks for help on this. If you pass the function through an alias template parameter then stringof on the alias gives an error, however using the type as pointed out above does indeed give the full string, which I then parse manually. Manually parsing the string is difficult if the types are complex or if the declaration consists of types only. I use this for automatically registering functions in a higher level interface callable with named function arguments.
    – John
    Commented Nov 3, 2010 at 11:05
  • @John: It might be worth while to just call out the argument names by hand. For one thing this would allow you to change the names to something clean without reworking the code.
    – BCS
    Commented Nov 3, 2010 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.