NamedTupledFunction

In Scala 3 we have TupledFunction which allows converting a function of N arguments to a function of a single tuple argument with arity N. Unfortunately, still experimental, but a great facility.

I’m wondering if we could use the same approach for named tuples. If we have a function foo(x: Int, y: Int) => Int, we could convert to ((x:Int, y: Int) => Int. Then, pass an argument as a regular tuple or with names.

My end goal would be to use this approach in Smockito, to improve readability and even allow hiding irrelevant arguments by picking the others by name.

Does this make sense? If so, I can try to write a pre-SIP for this.

Maybe it’s worth adding, I am not sure if the parameter names are lost during the eta expansion phase of a method which is my use case. If function types do not retain argument names, this can be tougher than I expected.

Function types do not retain parameter names, but if a method symbol is available, parameter names can be extracted from it. e.g. we do that in Functoid’s macro. But aforementioned Functoid had to be implemented as an implicit macro conversion, not a typeclass like TupledFunction, exactly because there’s not enough information in the function type for it and access to the tree itself was required. I suspect that NamedTupledFunction might face similar restrictions.

1 Like

That looks cool. An alternative to that would be creating a new function type with parameter names and have eta expansion yield it instead. Then I believe that restriction could be lifted. That would of course need to be implemented at the language level.