3

Is scala really designed and implemented so that there is no way to transform a tuple into something acceptable as an argument list? In How to pass a tuple argument the best way? it goes the other way around - transforming the argument accepting method. This does not seem to cover a trivial use case:

At the lack of such a way, code becomes clumsy for example when you try to extend a class that takes parameters, e.g. when you wish to extend an arguments-taking-class you can't modify the source code of. In such case you need to spawn out a factory object for overcoming the above limitation, as no function can be substituted for the argument list of the class being extended. So you work around it by creating a factory object that instantiates the class you wish to extend rather than just extending it, as a workaround. This kind of workaround is conspicuous and non-economical to me.

Thanks in advance.

Community
  • 1
  • 1
matanster
  • 15,072
  • 19
  • 88
  • 167

1 Answers1

4

This can be achieved using standard language features. You can define an extending class that only translates from a tuple to the arguments expected by the original class definition.

Example

 class TakeTwo(a: Int, b: Int)

 class TakeTwoTuple( t : (Int,Int) ) extends TakeTwo(t._1, t._2)


 scala> val tuple = (3,4)
 tuple: (Int, Int) = (3,4)

 scala> new TakeTwoTuple(tuple)
Community
  • 1
  • 1
Andreas Neumann
  • 10,734
  • 1
  • 32
  • 52