我很困惑如何将List / Seq / Array扩展为可变长度的参数列表。
鉴于我有test_func函数接受元组:
scala> def test_func(t:Tuple2[String,String]*) = println("works!")
test_func: (t: (String, String)*)Unit
当我传递元组时,哪个有效:
scala> test_func(("1","2"),("3","4"))
works!
通过阅读Scala参考资料,我得到了强烈的印象,即以下内容也可以起作用:
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")))
^
还有一次绝望的尝试:
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)
如何将List / Seq / Array扩展为参数列表?
提前谢谢!
答案 0 :(得分:46)
您需要添加:_*
。
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!