我正在做一些练习,并注意到以下匹配 tuple2 的行为。这有什么特别的原因吗?
def test(x: Any): Unit= x match{
case i: Int => println("int")
case b: Boolean => println("bool")
case ti: (_, Int) => println("tuple2 with int")
case tb: (_, Boolean)=> println("tuple2 with boolean")
case _ => println("other")
}
test(false) //prints bool
test(3) ///prints int
test((1,3)) //prints tuple with int
test((1,true)) //prints tuple with int
如果我交换 ti 和 tb 的情况,那么 (1,3) 用布尔值打印 tuple2。我认为这里正在进行一些类型转换,但我不清楚为什么。
有人可以给我一个快速的解释。谢谢