我感到困惑與階模式匹配行爲,參見下面的代碼:模式匹配java.lang.Long中
import java.util.concurrent.atomic.AtomicLong
object LongPatternMatching {
def main(args: Array[String]): Unit = {
useJavaLong
useScalaLong
useComplexJavaLong
}
def useScalaLong: Unit = {
val aLong: Long = Long.MaxValue
aLong match {
case v if v == Long.MinValue => println("min")
case v if v == Long.MaxValue => println("max")
}
}
def useJavaLong: Unit = {
val aLong: java.lang.Long = java.lang.Long.MAX_VALUE
aLong match {
case v if v == java.lang.Long.MIN_VALUE => println("min")
case v if v == java.lang.Long.MAX_VALUE => println("max")
}
}
def useComplexJavaLong: Unit = {
val counter: AtomicLong = new AtomicLong(0)
counter.incrementAndGet() match {
case count if count % 1000 == 0 => println(count)
}
}
}
首先兩個功能是好的,但第三(useComplexJavaLong)拋出scala.MatchError:1(的class java.lang.Long)