Tuple2的scala类型匹配

Tuple2的scala类型匹配,scala,types,match,Scala,Types,Match,我正在进行一些练习,并注意到以下匹配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

我正在进行一些练习,并注意到以下匹配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。我想这里正在进行某种类型的铸造,但我不清楚为什么

有人能给我一个快速的解释吗。
谢谢,请键入擦除。它无法在运行时判断
元组中的类型。它可以编译,但应该发出警告。这就是我在REPL中的
:粘贴
模式下执行此操作时发生的情况:

scala> :paste
// Entering paste mode (ctrl-D to finish)

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")
  }                                                

// Exiting paste mode, now interpreting.

<console>:10: warning: non-variable type argument Int in type pattern (_, Int) is unchecked since it is eliminated by erasure
         case ti: (_, Int) => println("tuple2 with int")
                  ^
<console>:11: warning: non-variable type argument Boolean in type pattern (_, Boolean) is unchecked since it is eliminated by erasure
         case tb: (_, Boolean)=> println("tuple2 with boolean")
                  ^
<console>:11: warning: unreachable code
         case tb: (_, Boolean)=> println("tuple2 with boolean")
                                        ^
test: (x: Any)Unit
scala>:粘贴
//进入粘贴模式(按ctrl-D键完成)
def测试(x:任意):单位=x匹配{
案例一:Int=>println(“Int”)
案例b:Boolean=>println(“bool”)
案例ti:(u,Int)=>println(“带Int的tuple2”)
案例tb:(u,Boolean)=>println(“带有Boolean的tuple2”)
案例=>println(“其他”)
}                                                
//正在退出粘贴模式,现在正在解释。
:10:警告:类型模式(u,Int)中的非变量类型参数Int未选中,因为它已通过擦除消除
案例ti:(u,Int)=>println(“带Int的tuple2”)
^
:11:警告:类型模式中的非变量类型参数布尔值(389;,布尔值)未选中,因为它已通过擦除消除
案例tb:(u,Boolean)=>println(“带有Boolean的tuple2”)
^
:11:警告:无法访问的代码
案例tb:(u,Boolean)=>println(“带有Boolean的tuple2”)
^
测试:(x:任意)单元

请注意最后一个警告,它说
(,Boolean)
无法访问,因为
(,Int)
将在每个
元组2
上匹配,这得益于类型擦除。

类型擦除。它无法在运行时判断
元组中的类型。它可以编译,但应该发出警告。这就是我在REPL中的
:粘贴
模式下执行此操作时发生的情况:

scala> :paste
// Entering paste mode (ctrl-D to finish)

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")
  }                                                

// Exiting paste mode, now interpreting.

<console>:10: warning: non-variable type argument Int in type pattern (_, Int) is unchecked since it is eliminated by erasure
         case ti: (_, Int) => println("tuple2 with int")
                  ^
<console>:11: warning: non-variable type argument Boolean in type pattern (_, Boolean) is unchecked since it is eliminated by erasure
         case tb: (_, Boolean)=> println("tuple2 with boolean")
                  ^
<console>:11: warning: unreachable code
         case tb: (_, Boolean)=> println("tuple2 with boolean")
                                        ^
test: (x: Any)Unit
def test(x: Any) {
x match {
  case xi: Int => println("int [" + xi + "]")
  case yb: Boolean => println("boolean [" + yb + "]")
  case (x, y @ (y1: Int)) => println("[" + x + ", int(" + y + ")]")
  case (x, y @ (y1: Boolean)) => println("[" + x + ", boolean(" + y + ")]")
  case _ => println("anything else")
}
scala>:粘贴
//进入粘贴模式(按ctrl-D键完成)
def测试(x:任意):单位=x匹配{
案例一:Int=>println(“Int”)
案例b:Boolean=>println(“bool”)
案例ti:(u,Int)=>println(“带Int的tuple2”)
案例tb:(u,Boolean)=>println(“带有Boolean的tuple2”)
案例=>println(“其他”)
}                                                
//正在退出粘贴模式,现在正在解释。
:10:警告:类型模式(u,Int)中的非变量类型参数Int未选中,因为它已通过擦除消除
案例ti:(u,Int)=>println(“带Int的tuple2”)
^
:11:警告:类型模式中的非变量类型参数布尔值(389;,布尔值)未选中,因为它已通过擦除消除
案例tb:(u,Boolean)=>println(“带有Boolean的tuple2”)
^
:11:警告:无法访问的代码
案例tb:(u,Boolean)=>println(“带有Boolean的tuple2”)
^
测试:(x:任意)单元
请注意最后一个警告,它说
(u,Boolean)
不可访问,因为
(u,Int)
将在每个
元组2
上匹配,这要归功于类型擦除

def test(x: Any) {
x match {
  case xi: Int => println("int [" + xi + "]")
  case yb: Boolean => println("boolean [" + yb + "]")
  case (x, y @ (y1: Int)) => println("[" + x + ", int(" + y + ")]")
  case (x, y @ (y1: Boolean)) => println("[" + x + ", boolean(" + y + ")]")
  case _ => println("anything else")
}
}

它似乎是这样工作的。 然而,单例(x,y@Int)不起作用,即使它符合要求

}

它似乎是这样工作的。
然而,单例(x,y@Int)不起作用,即使它符合要求

您可以试试这个,它只需对代码进行最小的更改即可正常工作。通过解包和键入,该函数运行良好

def test: Any => Unit = _ match{
  case i: Int => println("int")
  case b: Boolean => println("bool")
  case (x:Any, y: Boolean)=> println("tuple2 with boolean")
  case (x:Any, y: Int) => println("tuple2 with int")
  case _ => println("other")
}

您可以尝试一下,它只需对代码进行最小的更改即可正常工作。通过解包和键入,该函数运行良好

def test: Any => Unit = _ match{
  case i: Int => println("int")
  case b: Boolean => println("bool")
  case (x:Any, y: Boolean)=> println("tuple2 with boolean")
  case (x:Any, y: Int) => println("tuple2 with int")
  case _ => println("other")
}

啊,谢谢你!出于某种原因,eclipse没有发出此警告。我应该知道得更清楚,并在REPL中试用。谢谢迟到接受…我想迟到总比不接受好…对不起啊,谢谢!出于某种原因,eclipse没有发出此警告。我应该知道得更清楚,并在REPL中试用。谢谢迟到接受…迟到总比不接受好我想…很抱歉我很有意思。我想知道@then是否也可以通过强制scala在y上匹配来避免adelbertc提到的类型擦除。
y@(y1:Boolean)
可以写成
y@(\uu:Boolean)
可以写成
y:Boolean
我想最接近OP代码的是
case ti@(\uu,\uu:Int)=>嗯,很有趣。我想知道@then是否也可以通过强制scala在y上匹配来避免adelbertc提到的类型擦除。
y@(y1:Boolean)
可以写成
y@(\uu:Boolean)
可以写成
y:Boolean
我想最接近OP代码的是
case ti@(\uu,\uu:Int)=>