【发布时间】:2013-01-11 07:48:21
【问题描述】:
我正在做一些练习,并注意到以下匹配 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。我认为这里正在进行一些类型转换,但我不清楚为什么。
谁能给我一个快速的解释。 谢谢
【问题讨论】: