![](/img/trans.png)
[英]Why the scala :_* to expand a Seq into variable-length argument list does not work in this case?
[英]Scala: Expand List of Tuples into variable-length argument list of Tuples
我對如何將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)
如何將列表/序列/數組擴展為參數列表?
先感謝您!
您需要添加:_*
。
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!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.