Tom*_*ral 2 scala sequence variadic-functions
此代码将无法编译
val sortedSet = SortedSet[Int](Array(1,2,3,4).toSeq)
Error: type mismatch; found :Seq[Int] required Int
Run Code Online (Sandbox Code Playgroud)
但是,以下是SortedSet中apply的定义:
def apply[A](elems: A*)(implicit ord: Ordering[A]): CC[A] = (newBuilder[A](ord) ++= elems).result
Run Code Online (Sandbox Code Playgroud)
它说elem是一个vararg因此应该是Seq [A]类型我错过了什么?为什么我不能通过seq作为vararg?
只需添加 : _*
scala> SortedSet[Int](Array(1,2,3,4).toSeq: _*)
res2: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2, 3, 4)
Run Code Online (Sandbox Code Playgroud)