digitalmars.D - Is there a way to get the names of a function's parameters?
- Adam Ruppe (12/12) Jun 13 2010 Given:
- BCS (5/15) Jun 13 2010 No need to parse the whole source:
- Jacob Carlborg (5/17) Jun 14 2010 Here you go: http://tango.pastebin.com/M38jdhGd including calling using
- Simen kjaeraas (12/14) Jun 14 2010 Does not handle function/delegate return types.
- Simen kjaeraas (20/22) Jun 14 2010 string parameterNamesOf( alias fn )( ) {
- Jacob Carlborg (4/22) Jun 15 2010 Thanks.
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
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
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
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
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
On 2010-06-14 15:47, Simen kjaeraas wrote:Simen kjaeraas <simen.kjaras gmail.com> wrote:Thanks. -- /Jacob CarlborgOne 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]; }
Jun 15 2010