2013-08-05 54 views
2

一個可變參數的這個代碼將無法編譯傳遞序號[T]在斯卡拉

val sortedSet = SortedSet[Int](Array(1,2,3,4).toSeq) 

    Error: type mismatch; found :Seq[Int] required Int 

然而,這裏是適用於SortedSet的定義:

def apply[A](elems: A*)(implicit ord: Ordering[A]): CC[A] = (newBuilder[A](ord) ++= elems).result 

它說ELEM是vararg,因此在應該是類型Seq [A] 我錯過了什麼?爲什麼我不能通過Seq作爲可變參數?

回答

6

只需添加: _*

scala> SortedSet[Int](Array(1,2,3,4).toSeq: _*) 
res2: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2, 3, 4)