Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
June 13, 2010 Is there a way to get the names of a function's parameters? | ||||
---|---|---|---|---|
| ||||
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. |
June 14, 2010 Re: Is there a way to get the names of a function's parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam Ruppe | 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>< |
June 14, 2010 Re: Is there a way to get the names of a function's parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam Ruppe | 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 |
June 14, 2010 Re: Is there a way to get the names of a function's parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | 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 |
June 14, 2010 Re: Is there a way to get the names of a function's parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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 |
June 15, 2010 Re: Is there a way to get the names of a function's parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | 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 |
Copyright © 1999-2021 by the D Language Foundation