Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Expand List of Tuples into variable-length argument list of Tuples

I'm puzzled on how to expand List/Seq/Array into variable-length argument list.

Given that I have test_func function accepting tuples:

scala> def test_func(t:Tuple2[String,String]*) = println("works!")
test_func: (t: (String, String)*)Unit

Which works when I pass tuples:

scala> test_func(("1","2"),("3","4"))
works!

From reading the Scala reference I've got strong impression that the following whould work as well:

scala> test_func(List(("1","2"),("3","4")))
<console>:9: error: type mismatch;
 found   : List[(java.lang.String, java.lang.String)]
 required: (String, String)
              test_func(List(("1","2"),("3","4")))
                        ^

And one more desperate attempt:

scala> test_func(List(("1","2"),("3","4")).toSeq)
<console>:9: error: type mismatch;
 found   : scala.collection.immutable.Seq[(java.lang.String, java.lang.String)]
 required: (String, String)
              test_func(List(("1","2"),("3","4")).toSeq)

How to expand List/Seq/Array into argument list?

Thank you in advance!

like image 552
forker Avatar asked Jun 01 '12 00:06

forker


1 Answers

You need to add :_*.

scala> test_func(List(("1","2"),("3","4")):_*)
works!
scala> test_func(Seq(("1","2"),("3","4")):_*)
works!
scala> test_func(Array(("1","2"),("3","4")):_*)
works!
like image 188
Prince John Wesley Avatar answered Nov 20 '22 22:11

Prince John Wesley