www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - tupleof function parameters?

reply Jon Degenhardt <jond noreply.com> writes:
I'd like to create a Tuple alias representing a function's 
parameter list. Is there a way to do this?

Here's an example creating a Tuple alias for a function's 
parameters by hand:

     import std.typecons: Tuple;

     bool fn(string op, int v1, int v2)
     {
         switch (op)
         {
         default: return false;
         case "<": return v1 < v2;
         case ">": return v1 > v2;
         }
     }

     alias fnArgs = Tuple!(string, "op", int, "v1", int, "v2");

     unittest
     {
         auto args = fnArgs("<", 3, 5);
         assert(fn(args[]));
     }

This is quite useful. I'm wondering if there is a way to create 
the 'fnArgs' alias from the definition of 'fn' without needing to 
manually write out the '(string, "op", int, "v1", int, "v2")' 
sequence by hand. Something like a 'tupleof' operation on the 
function parameter list. Or conversely, define the tuple and use 
it when defining the function.

The goal is to write the argument list once and use it to create 
both the function and the Tuple alias. That way I could create a 
large number of these function / arglist tuple pairs with less 
brittleness.

--Jon
Aug 27 2018
next sibling parent reply Sebastiaan Koppe <mail skoppe.eu> writes:
On Tuesday, 28 August 2018 at 06:11:35 UTC, Jon Degenhardt wrote:
 The goal is to write the argument list once and use it to 
 create both the function and the Tuple alias. That way I could 
 create a large number of these function / arglist tuple pairs 
 with less brittleness.

 --Jon
I would probably use a combination of std.traits.Parameters and std.traits.ParameterIdentifierTuple. Parameters returns a tuple of types and ParameterIdentifierTuple returns a tuple of strings. Maybe you'll need to implement a staticZip to interleave both tuples to get the result you want. (although I remember seeing one somewhere).
Aug 27 2018
next sibling parent Jon Degenhardt <jond noreply.com> writes:
On Tuesday, 28 August 2018 at 06:20:37 UTC, Sebastiaan Koppe 
wrote:
 On Tuesday, 28 August 2018 at 06:11:35 UTC, Jon Degenhardt 
 wrote:
 The goal is to write the argument list once and use it to 
 create both the function and the Tuple alias. That way I could 
 create a large number of these function / arglist tuple pairs 
 with less brittleness.

 --Jon
I would probably use a combination of std.traits.Parameters and std.traits.ParameterIdentifierTuple. Parameters returns a tuple of types and ParameterIdentifierTuple returns a tuple of strings. Maybe you'll need to implement a staticZip to interleave both tuples to get the result you want. (although I remember seeing one somewhere).
Alex, Sebastiaan - Thanks much, this looks like it should get me what I'm looking for. --Jon
Aug 27 2018
prev sibling parent aliak <something something.com> writes:
On Tuesday, 28 August 2018 at 06:20:37 UTC, Sebastiaan Koppe 
wrote:
 On Tuesday, 28 August 2018 at 06:11:35 UTC, Jon Degenhardt 
 wrote:
 The goal is to write the argument list once and use it to 
 create both the function and the Tuple alias. That way I could 
 create a large number of these function / arglist tuple pairs 
 with less brittleness.

 --Jon
I would probably use a combination of std.traits.Parameters and std.traits.ParameterIdentifierTuple. Parameters returns a tuple of types and ParameterIdentifierTuple returns a tuple of strings. Maybe you'll need to implement a staticZip to interleave both tuples to get the result you want. (although I remember seeing one somewhere).
There's one in here -> https://code.dlang.org/packages/bolts And the docs have a link to the particular forum post you're thinking of as well for details.
Aug 28 2018
prev sibling parent Alex <sascha.orlov gmail.com> writes:
On Tuesday, 28 August 2018 at 06:11:35 UTC, Jon Degenhardt wrote:
 I'd like to create a Tuple alias representing a function's 
 parameter list. Is there a way to do this?

 [...]
Are you aware of https://dlang.org/phobos/std_traits.html#Parameters
Aug 27 2018