Can scala splat be used for anything that isn't a varargs?

gfxmonk picture gfxmonk · Apr 13, 2011 · Viewed 7k times · Source

given e.g:

scala> def pipes(strings:String*) = strings.toList.mkString("|")

which I can call normally:

scala> pipes("foo", "bar")
res1: String = foo|bar

or with a splat:

scala> val args = List("a","b","c")
scala> pipes(args:_*)
res2: String = a|b|c

But can I use a splat to provide arguments for anything but a varargs parameter? e.g I would like to do something like:

scala> def pipeItAfterIncrementing(i:Int, s:String) = (i + 1) + "|" + s
scala> val args:Tuple2[Int, String] = (1, "two")
scala> pipeItAfterIncrementing(args:_*)

That doesn't work, but is there any way to achieve the same effect of providing multiple arguments from a single object, whether it be a tuple or something else? Is there any reason this couldn't be implemented for tuples, given that both their length and types are known at compile-time?

Answer

Jean-Philippe Pellet picture Jean-Philippe Pellet · Apr 13, 2011

You can use Function.tupled to do exactly this: turn a function that takes n arguments into a function that takes a single tuple argument of arity n. As can be expected, Function.untupled does the reverse job.

The special type ascription : _* is only applicable for repeated parameter (a.k.a. varargs).

scala> def pipeItAfterIncrementing(i:Int, s:String) = (i + 1) + "|" + s
pipeItAfterIncrementing: (i: Int,s: String)java.lang.String

scala> def tupledPipeItAfterIncrementing = Function.tupled(pipeItAfterIncrementing _)
tupledPipeItAfterIncrementing: ((Int, String)) => java.lang.String

scala> val args:Tuple2[Int, String] = (1, "two")
args: (Int, String) = (1,two)

scala> tupledPipeItAfterIncrementing(args)
res0: java.lang.String = 2|two