tupleof function parameters?

Jon Degenhardt jond at noreply.com
Tue Aug 28 06:11:35 UTC 2018


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



More information about the Digitalmars-d-learn mailing list