www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is there a way to get the names of a function's parameters?

reply Adam Ruppe <destructionator gmail.com> writes:
Given:

void foo(int a, string b);

You can use std.traits.ParameterTypeTuple to get (int, string).

Is there any method, at all, to get ("a", "b") out of it? I can't find
one, and am considering import("mysrc.d"); and finding it that way,
but figured I'd ask first.

The benefits of getting the names would be runtime function calls, or
named arguments. Consider this:

ParameterTuple!foo args;

args.b = "hello";

foo(args);

That'd be kinda cool.
Jun 13 2010
next sibling parent BCS <none anon.com> writes:
Hello Adam,

 Given:
 
 void foo(int a, string b);
 
 You can use std.traits.ParameterTypeTuple to get (int, string).
 
 Is there any method, at all, to get ("a", "b") out of it? I can't find
 one, and am considering import("mysrc.d"); and finding it that way,
 but figured I'd ask first.
 
No need to parse the whole source: http://codepad.org/ozZzC98d -- ... <IXOYE><
Jun 13 2010
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2010-06-14 00:56, Adam Ruppe wrote:
 Given:

 void foo(int a, string b);

 You can use std.traits.ParameterTypeTuple to get (int, string).

 Is there any method, at all, to get ("a", "b") out of it? I can't find
 one, and am considering import("mysrc.d"); and finding it that way,
 but figured I'd ask first.

 The benefits of getting the names would be runtime function calls, or
 named arguments. Consider this:

 ParameterTuple!foo args;

 args.b = "hello";

 foo(args);

 That'd be kinda cool.
Here you go: http://tango.pastebin.com/M38jdhGd including calling using named arguments. -- /Jacob Carlborg
Jun 14 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Jacob Carlborg <doob me.com> wrote:

 Here you go: http://tango.pastebin.com/M38jdhGd including calling using  
 named arguments.
Does not handle function/delegate return types. struct S {} void function(int delegate(float)) F(string delegate(), string function(string, string), float, double, S); writeln("Parameters: ", parameterNamesOf!F); Gives: Parameters: delegate(float One needs to match parentheses from the last parentheses in the typeof.stringof. -- Simen
Jun 14 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Simen kjaeraas <simen.kjaras gmail.com> wrote:

 One needs to match parentheses from the last parentheses in the  
 typeof.stringof.
string parameterNamesOf( alias fn )( ) { string fullName = typeof(&fn).stringof; int pos = fullName.lastIndexOf( ')' ); int end = pos; int count = 0; do { if ( fullName[pos] == ')' ) { count++; } else if ( fullName[pos] == '(' ) { count--; } pos--; } while ( count > 0 ); return fullName[pos+2..end]; } -- Simen
Jun 14 2010
parent Jacob Carlborg <doob me.com> writes:
On 2010-06-14 15:47, Simen kjaeraas wrote:
 Simen kjaeraas <simen.kjaras gmail.com> wrote:

 One needs to match parentheses from the last parentheses in the
 typeof.stringof.
string parameterNamesOf( alias fn )( ) { string fullName = typeof(&fn).stringof; int pos = fullName.lastIndexOf( ')' ); int end = pos; int count = 0; do { if ( fullName[pos] == ')' ) { count++; } else if ( fullName[pos] == '(' ) { count--; } pos--; } while ( count > 0 ); return fullName[pos+2..end]; }
Thanks. -- /Jacob Carlborg
Jun 15 2010