digitalmars.D.learn - tupleof function parameters?
- Jon Degenhardt (31/31) Aug 27 2018 I'd like to create a Tuple alias representing a function's
- Sebastiaan Koppe (7/12) Aug 27 2018 I would probably use a combination of std.traits.Parameters and
- Jon Degenhardt (4/19) Aug 27 2018 Alex, Sebastiaan - Thanks much, this looks like it should get me
- aliak (5/20) Aug 28 2018 There's one in here -> https://code.dlang.org/packages/bolts
- Alex (3/6) Aug 27 2018 Are you aware of
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
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. --JonI 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
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:Alex, Sebastiaan - Thanks much, this looks like it should get me what I'm looking for. --JonThe 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. --JonI 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
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: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.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. --JonI 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 28 2018
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