Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
September 03, 2013 Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
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? |
September 03, 2013 Re: Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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. |
September 03, 2013 Re: Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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. :)
|
September 03, 2013 Re: Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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.
|
September 04, 2013 Re: Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 |
September 04, 2013 Re: Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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, ...).
|
September 04, 2013 Re: Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 |
September 07, 2013 Re: Using traits how do i get a function's parameters as a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Thanks all! :) |
Copyright © 1999-2021 by the D Language Foundation