Replacing the parameters of a function with a vector

Viewed 97

In Scala, if I have a function f(X, Y, Z) is it possible to pass a variable W that will represent (X, Y, Z) so I can use f(W) instead of f(X,Y,Z)? If so, how can this be done?

1 Answers

If you mean to pass a tuple or case class instead of the three parameters. Then yes, you can.

First, If it is a function, you can just: f.tupled(tuple).
If it is a method then: (m _).tupled(tuple). _(it is basically the same, but using eta-expansion to turn the method into a function).

If you have a case class instead of a tuple, you can to turn an instance of it into a tuple with this: W.unapply(w).get (where W is the companion object of the case class, and w is the instance).

Related