www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Obtaining argument names in (variadic) functions

reply data pulverizer <data.pulverizer gmail.com> writes:
Hi D gurus,

is there a way to obtain parameter names within the function 
body? I am particularly interested in variadic functions. 
Something like:

void myfun(T...)(T x){
     foreach(i, arg; x)
         writeln(i, " : ", arg);
}

void main(){
     myfun(a = 2, b = "two", c = 2.0);
}

// should print
a : 2
b : two
c : 2.0

Thanks in advance

Loving the mixins and tuples
Mar 16 2016
next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 3/16/16 4:24 PM, data pulverizer wrote:
 Hi D gurus,

 is there a way to obtain parameter names within the function body? I am
 particularly interested in variadic functions. Something like:

 void myfun(T...)(T x){
      foreach(i, arg; x)
          writeln(i, " : ", arg);
 }

 void main(){
      myfun(a = 2, b = "two", c = 2.0);
 }
This isn't valid code. The name of the parameters is x[0], x[1], and x[2]. You could do something like: myfun("a", 2, "b", "two", "c", 2.0); and process it properly. -Steve
Mar 16 2016
prev sibling next sibling parent reply jkpl <jkpl jkpl.de> writes:
On Wednesday, 16 March 2016 at 20:24:38 UTC, data pulverizer 
wrote:
 Hi D gurus,

 is there a way to obtain parameter names within the function 
 body? I am particularly interested in variadic functions. 
 Something like:

 void myfun(T...)(T x){
     foreach(i, arg; x)
         writeln(i, " : ", arg);
 }

 void main(){
     myfun(a = 2, b = "two", c = 2.0);
 }

 // should print
 a : 2
 b : two
 c : 2.0

 Thanks in advance

 Loving the mixins and tuples
I try to anticipate the reason why you want this. As said in a previous answer you can access to an individual element by using the array syntax but also _param_<X>, with X the index of the parameter: void myfun(T...)(T x) { import std.traits; import std.stdio; writeln(ParameterIdentifierTuple!(myfun!T)); writeln(_param_0); writeln(_param_1); writeln(_param_2); } void main() { int a=1,b=2,c=3; myfun(a,b,c); }
Mar 16 2016
parent reply JR <sunspyre gmail.com> writes:
On Wednesday, 16 March 2016 at 20:43:09 UTC, jkpl wrote:
 I try to anticipate the reason why you want this. [...]
I use something *kinda* sort of similar in my toy project to print all fields of a struct, for debugging purposes when stuff goes wrong. Getting the names of the member variables is crucial then. http://dpaste.dzfl.pl/748c4dd97de6
Mar 16 2016
parent data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 16 March 2016 at 21:05:43 UTC, JR wrote:
 On Wednesday, 16 March 2016 at 20:43:09 UTC, jkpl wrote:
 I try to anticipate the reason why you want this. [...]
I use something *kinda* sort of similar in my toy project to print all fields of a struct, for debugging purposes when stuff goes wrong. Getting the names of the member variables is crucial then. http://dpaste.dzfl.pl/748c4dd97de6
That's a nice learning piece. I think "with" is cool, reminds me of a nice R feature.
Mar 16 2016
prev sibling parent reply JR <sunspyre gmail.com> writes:
On Wednesday, 16 March 2016 at 20:24:38 UTC, data pulverizer 
wrote:
 Hi D gurus,

 is there a way to obtain parameter names within the function 
 body? I am particularly interested in variadic functions. 
 Something like:

 void myfun(T...)(T x){
     foreach(i, arg; x)
         writeln(i, " : ", arg);
 }

 void main(){
     myfun(a = 2, b = "two", c = 2.0);
 }

 // should print
 a : 2
 b : two
 c : 2.0

 Thanks in advance

 Loving the mixins and tuples
You can do it precisely like that if the variables/symbols you pass as (template) arguments are properly declared first. http://dpaste.dzfl.pl/0b452efeaaab void printVars(Args...)() if (Args.length > 0) { import std.stdio : writefln; foreach (i, arg; Args) { writefln("%s\t%s:\t%s", typeof(Args[i]).stringof, Args[i].stringof, arg); } } void main() { int abc = 3; string def = "58"; float ghi = 3.14f; double jkl = 3.14; printVars!(abc,def,ghi,jkl)(); }
Mar 16 2016
next sibling parent data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 16 March 2016 at 20:53:42 UTC, JR wrote:
 On Wednesday, 16 March 2016 at 20:24:38 UTC, data pulverizer 
 wrote:
 Hi D gurus,

 is there a way to obtain parameter names within the function 
 body? I am particularly interested in variadic functions. 
 Something like:

 void myfun(T...)(T x){
     foreach(i, arg; x)
         writeln(i, " : ", arg);
 }

 void main(){
     myfun(a = 2, b = "two", c = 2.0);
 }

 // should print
 a : 2
 b : two
 c : 2.0

 Thanks in advance

 Loving the mixins and tuples
You can do it precisely like that if the variables/symbols you pass as (template) arguments are properly declared first. http://dpaste.dzfl.pl/0b452efeaaab void printVars(Args...)() if (Args.length > 0) { import std.stdio : writefln; foreach (i, arg; Args) { writefln("%s\t%s:\t%s", typeof(Args[i]).stringof, Args[i].stringof, arg); } } void main() { int abc = 3; string def = "58"; float ghi = 3.14f; double jkl = 3.14; printVars!(abc,def,ghi,jkl)(); }
That's brilliant! Thanks JR
Mar 16 2016
prev sibling parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Wednesday, 16 March 2016 at 20:53:42 UTC, JR wrote:
 void printVars(Args...)()
 if (Args.length > 0)
 {
     import std.stdio : writefln;

     foreach (i, arg; Args) {
         writefln("%s\t%s:\t%s", typeof(Args[i]).stringof, 
 Args[i].stringof, arg);
     }
 }

 void main() {
     int abc = 3;
     string def = "58";
     float ghi = 3.14f;
     double jkl = 3.14;

     printVars!(abc,def,ghi,jkl)();
 }
Interesting, any idea if it is possible to do assignment within template.. Either: printVars!(int abc=5,string def="58")(); or something like printVars!("abc","def",ghi)(5,"58");
Mar 17 2016
parent reply JR <sunspyre gmail.com> writes:
On Thursday, 17 March 2016 at 11:52:13 UTC, Edwin van Leeuwen 
wrote:
 On Wednesday, 16 March 2016 at 20:53:42 UTC, JR wrote:
 void printVars(Args...)()
 if (Args.length > 0)
 {
     import std.stdio : writefln;

     foreach (i, arg; Args) {
         writefln("%s\t%s:\t%s", typeof(Args[i]).stringof, 
 Args[i].stringof, arg);
     }
 }

 void main() {
     int abc = 3;
     string def = "58";
     float ghi = 3.14f;
     double jkl = 3.14;

     printVars!(abc,def,ghi,jkl)();
 }
Interesting, any idea if it is possible to do assignment within template.. Either: printVars!(int abc=5,string def="58")(); or something like printVars!("abc","def",ghi)(5,"58");
What would the use-cases for those be? I don't think the first is valid grammar, and I'm not sure what you want the second to do. Resolve symbols by string literals of their names? That might need a string mixin as they wouldn't be in scope when in the called template function, but I've never tried it. You *can* cook up something that modifies the values of variables you pass in -- like modifyVars!(abc,def,ghi)("asdf", 123, 3.14) -- but you just might be better off with runtime ref parameters then.
Mar 17 2016
parent reply Edwin van Leeuwen <edder tkwsping.nl> writes:
On Thursday, 17 March 2016 at 13:53:00 UTC, JR wrote:
 Interesting, any idea if it is possible to do assignment 
 within template.. Either:

 printVars!(int abc=5,string def="58")();
 or something like
 printVars!("abc","def",ghi)(5,"58");
What would the use-cases for those be? I don't think the first is valid grammar, and I'm not sure what you want the second to do. Resolve symbols by string literals of their names? That might need a string mixin as they wouldn't be in scope when in the called template function, but I've never tried it.
Both use cases are when you want a named parameter, without having to assign it first. I know the first is not valid grammar, was just wondering if you might be smarter than me and see a way to make it valid :) Second one is another possible alternative that I have been thinking about. Basically, say I want to have the named (optional) parameters x and y. In your initial example I would be required to do: ``` int x = 1; string y = "2"; doSomethingWithNamedPars!(x,y)(); ``` I just hoped to shorten that to a one liner similar to: ``` doSomethingWithNamedPars!(x=1,y="2")(); ``` or alternatively ``` doSomethingWithNamedPars!("x","y")(1,"2"); ``` (where doSomethingWithNamedPars's behaviour depends on which named parameters it is passed) Just as a reference, my current approach (in ggplotd) is with named tuples, but that is slightly more verbose than I had hoped: ``` doSomethingWithNamedPars( Tuple!(int, "x", string, "y")( 1, 2 ) ); ```
Mar 17 2016
parent JR <sunspyre gmail.com> writes:
On Thursday, 17 March 2016 at 14:12:38 UTC, Edwin van Leeuwen 
wrote:
 On Thursday, 17 March 2016 at 13:53:00 UTC, JR wrote:
 Interesting, any idea if it is possible to do assignment 
 within template.. Either:

 printVars!(int abc=5,string def="58")();
 or something like
 printVars!("abc","def",ghi)(5,"58");
What would the use-cases for those be? I don't think the first is valid grammar, and I'm not sure what you want the second to do. Resolve symbols by string literals of their names? That might need a string mixin as they wouldn't be in scope when in the called template function, but I've never tried it.
Both use cases are when you want a named parameter, without having to assign it first. I know the first is not valid grammar, was just wondering if you might be smarter than me and see a way to make it valid :) Second one is another possible alternative that I have been thinking about. Basically, say I want to have the named (optional) parameters x and y. In your initial example I would be required to do: ``` int x = 1; string y = "2"; doSomethingWithNamedPars!(x,y)(); ``` I just hoped to shorten that to a one liner similar to: ``` doSomethingWithNamedPars!(x=1,y="2")(); ``` or alternatively ``` doSomethingWithNamedPars!("x","y")(1,"2"); ``` (where doSomethingWithNamedPars's behaviour depends on which named parameters it is passed) Just as a reference, my current approach (in ggplotd) is with named tuples, but that is slightly more verbose than I had hoped: ``` doSomethingWithNamedPars( Tuple!(int, "x", string, "y")( 1, 2 ) ); ```
http://dpaste.dzfl.pl/1625122e4f01 is the best I can do, and it's ugly. It's something akin to your doSomethingWithNamedPars!("x","y")(1,"2"). I may even have missed the point. It *neccessitates* that the called function knows the parameter names, because they're not self-documented in the signature. It will also have to deal with any missing ones to actually compile. I'm not sure how to do this and still enjoy things like const and ref and friends. I imagine there *might* be a way to do this with normal non-templated functions given a similar wrapper, if the functions are annotated so that it can get the names of the parameters. It feels like you should be able to get it to naïvely reorder the arguments, default-initialise any missing ones, etc. But you quickly run into "xyz cannot be read at compile-time", even when it feels like everything should be known.
Mar 20 2016