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!
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With