在這個例子中不會發生:從陣列到WrappedArray隱式轉換時的模式匹配
scala> val a: Seq[Int] = Array(1,2,3)
a: Seq[Int] = WrappedArray(1, 2, 3)
隱式轉換髮生,Array
轉換爲WrappedArray
延伸Seq
,如下解釋:Arrays Scala Doc
但這裏:
object MyObject {
def main(args: Array[String]): Unit = {
val a = Array(1,2,3)
// val a: Seq[Int] = Array(1,2,3) if added explicitly works
val result = a match {
case Seq(
first,
second,
third
) => (first, second, third)
}
println(result)
}
}
該代碼失敗:
Error:(9, 15) scrutinee is incompatible with pattern type;
found : Seq[A]
required: Array[Int]
case Seq(
隱式轉換不會發生,爲什麼?
作爲一個實用的解決方案,您可以隨時「檢索」您的數組。 'toSeq'方法不會構造一個新的集合,所以它只會包裝數組。來源:http://www.scala-lang.org/api/2.12.1/scala/Array.html#toSeq:Seq [A] –