Re: Is there a way to get the names of a function's parameters?

2010-06-15 Thread Jacob Carlborg

On 2010-06-14 15:47, Simen kjaeraas wrote:

Simen kjaeraas  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


Re: Is there a way to get the names of a function's parameters?

2010-06-14 Thread Simen kjaeraas

Simen kjaeraas  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


Re: Is there a way to get the names of a function's parameters?

2010-06-14 Thread Simen kjaeraas

Jacob Carlborg  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


Re: Is there a way to get the names of a function's parameters?

2010-06-14 Thread Jacob Carlborg

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


Re: Is there a way to get the names of a function's parameters?

2010-06-13 Thread BCS

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

--
... <