www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using traits how do i get a function's parameters as a string?

reply "Gary Willoughby" <dev nomad.so> writes:
Using traits how do i get a methods's parameters as a string? Say 
i have the following method:

...
public void setAge(int age)
{
     this._age = age;
}
...

I want a string that is: "(int age)" or how ever many params 
there are. The nearest i got was using this code:

ParameterTypeTuple!(__traits(getMember, T, "setAge")).stringof

Which yieds: "(int)"

Any ideas?
Sep 03 2013
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 3 September 2013 at 21:01:27 UTC, Gary Willoughby 
wrote:
 Using traits how do i get a methods's parameters as a string? 
 Say i have the following method:
Use typeof(setAge).stringof to get something you can then parse to fet the names. My web.d has a function to do it: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/web.d#L1912 InfoImpl is here: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/web.d#L1956 It gets typeof(func).stringof then does a rudimentary parsing to get the argument names and default values out as arrays of strings.
Sep 03 2013
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/3/13, Gary Willoughby <dev nomad.so> wrote:
 Using traits how do i get a methods's parameters as a string? Say
 i have the following method:
Here's a first attempt: ----- import std.range; import std.string; import std.stdio; import std.traits; class C { void setAge(int age, int) { } } template GetParamsString(alias func) { string getParamsString() { string[] paramTypes; string[] paramNames; foreach (id; ParameterTypeTuple!func) paramTypes ~= id.stringof; foreach (id; ParameterIdentifierTuple!func) paramNames ~= id; string[] result; foreach (type, name; zip(paramTypes, paramNames)) result ~= format("%s %s", type, name); return format("(%s)", result.join(", ")); } enum GetParamsString = getParamsString(); } void main() { string x = GetParamsString!(C.setAge); writeln(x); } ----- Others will find a few ways to simplify this, I'm sure. :)
Sep 03 2013
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Tuesday, 3 September 2013 at 21:20:04 UTC, Andrej Mitrovic 
wrote:
         foreach (id; ParameterIdentifierTuple!func)
How do I keep missing these new std.traits things? Very nice.
Sep 03 2013
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-09-03 23:32, Adam D. Ruppe wrote:

 How do I keep missing these new std.traits things? Very nice.
Because they just magically shows up :) -- /Jacob Carlborg
Sep 03 2013
prev sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/3/13, Adam D. Ruppe <destructionator gmail.com> wrote:
 On Tuesday, 3 September 2013 at 21:20:04 UTC, Andrej Mitrovic
 wrote:
         foreach (id; ParameterIdentifierTuple!func)
How do I keep missing these new std.traits things? Very nice.
It's really funky that an is() expression is used to extract this: static if (is(FunctionTypeOf!func PT == __parameters)) { } I'd assume it would be __traits(getParams, ...).
Sep 04 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-09-04 13:54, Andrej Mitrovic wrote:

 It's really funky that an is() expression is used to extract this:

 static if (is(FunctionTypeOf!func PT == __parameters)) { }

 I'd assume it would be __traits(getParams, ...).
I agree. Wonder why this approach was chosen. -- /Jacob Carlborg
Sep 04 2013
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
Thanks all! :)
Sep 07 2013