digitalmars.D.learn - Using traits how do i get a function's parameters as a string?
- Gary Willoughby (13/13) Sep 03 2013 Using traits how do i get a methods's parameters as a string? Say
- Adam D. Ruppe (11/13) Sep 03 2013 Use typeof(setAge).stringof to get something you can then parse
- Andrej Mitrovic (37/39) Sep 03 2013 Here's a first attempt:
- Adam D. Ruppe (3/4) Sep 03 2013 How do I keep missing these new std.traits things? Very nice.
- Jacob Carlborg (4/5) Sep 03 2013 Because they just magically shows up :)
- Andrej Mitrovic (4/8) Sep 04 2013 It's really funky that an is() expression is used to extract this:
- Jacob Carlborg (4/7) Sep 04 2013 I agree. Wonder why this approach was chosen.
- Gary Willoughby (1/1) Sep 07 2013 Thanks all! :)
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
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
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
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
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
On 9/3/13, Adam D. Ruppe <destructionator gmail.com> wrote:On Tuesday, 3 September 2013 at 21:20:04 UTC, 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, ...).foreach (id; ParameterIdentifierTuple!func)How do I keep missing these new std.traits things? Very nice.
Sep 04 2013
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