Scala 模式匹配返回类型的探讨
val arr = Array(1,2)
val res = arr match {
case Array(x,y) => Array(y,x)
case Array(x,y,z) => Array(0)
case _ => Array(-1)
}
对于上述代码,由于所有的 case 返回的均是 Array[Int],所有 res 的类型就是 Array[Int]。
将上述代码进行修改,某个 case 返回 Array[String]
val arr...
原创
2020-07-24 20:20:18 ·
379 阅读 ·
0 评论