New Issue: tuples and named arguments

16388, “mppf”, “tuples and named arguments”, “2020-09-15T13:29:51Z”

Tuples have a relationship with argument passing and varargs functions. A tuple can bundle up all of the arguments passed to one function and then pass them to another. In this way, a varargs function can serve as a kind of wrapper to another function that passes along all of the arguments.

For example:

proc f(x: int, y: int) { writeln(x,y); }

proc callf(args...) { // accepts any number of arguments into a tuple
  f( (...args) );    // passes those arguments to f
}

callf(1, 2); // OK - prints out 12

This relationship is also syntactical; in callf(1,2), the syntax (1,2) can be thought of constructing a tuple that is passed to callf.

However, such a wrapper function cannot currently support named argument passing:

callf(y=2, x=1); // compilation failure

This issue asks if tuples should have optional names for the elements.

Adding optional names for tuple elements has some likely implications, beyond allowing the above code:

  • (y=2, x=1) would be a way to construct a tuple with named elements
  • presumably it would be possible to extract elements of tuple by name, e.g. (y=2, x=1).x
  • The names of elements in the tuple would form part of the type. In the case of proc callf(args...) - the args argument is generic and so would accept a tuple with named elements or not depending on the call site.
  • Perhaps tuples with different named arguments could assign to each other; or perhaps such behavior would require first converting the tuple to one without named elements.