3

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?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98
Jose Cabrera Zuniga
  • 2,348
  • 3
  • 31
  • 56
  • 1
    Possible duplicate of [Is there a Scala equivalent of the Python list unpack (a.k.a. "\*") operator?](https://stackoverflow.com/questions/15034565/is-there-a-scala-equivalent-of-the-python-list-unpack-a-k-a-operator) – Mario Galic Jun 18 '19 at 08:20

1 Answers1

3

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).