Scala模式匹配与Option [Any]的混淆

Jus*_*s12 33 scala pattern-matching unchecked actor

我有以下Scala代码.

import scala.actors.Actor

object Alice extends Actor {
  this.start
  def act{
    loop{
      react {
        case "Hello" => sender ! "Hi"
        case i:Int => sender ! 0
      }
    }
  }
}
object Test {
  def test = {
    (Alice !? (100, "Hello")) match {
      case i:Some[Int] => println ("Int received "+i)
      case s:Some[String] => println ("String received "+s)
      case _ =>
    }
    (Alice !? (100, 1)) match {
      case i:Some[Int] => println ("Int received "+i)
      case s:Some[String] => println ("String received "+s)
      case _ =>
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

完成后Test.test,我得到输出:

scala> Test.test
Int received Some(Hi)
Int received Some(0)
Run Code Online (Sandbox Code Playgroud)

我期待着输出

String received Some(Hi)
Int received Some(0)
Run Code Online (Sandbox Code Playgroud)

解释是什么?

作为第二个问题,我收到unchecked上述警告如下:

C:\scalac -unchecked a.scala
a.scala:17: warning: non variable type-argument Int in type pattern Some[Int] is unchecked since it is eliminated by erasure
      case i:Some[Int] => println ("Int received "+i)
             ^
a.scala:18: warning: non variable type-argument String in type pattern Some[String] is unchecked since it is eliminated by erasure
      case s:Some[String] => println ("String received "+s)
             ^
a.scala:22: warning: non variable type-argument Int in type pattern Some[Int] is unchecked since it is eliminated by erasure
      case i:Some[Int] => println ("Int received "+i)
             ^
a.scala:23: warning: non variable type-argument String in type pattern Some[String] is unchecked since it is eliminated by erasure
      case s:Some[String] => println ("String received "+s)
             ^
four warnings found
Run Code Online (Sandbox Code Playgroud)

我该如何避免警告?

编辑:感谢您的建议.Daniel的想法很好,但似乎不适用于泛型类型,如下例所示

def test[T] = (Alice !? (100, "Hello")) match { 
   case Some(i: Int) => println ("Int received "+i) 
   case Some(t: T) => println ("T received ") 
   case _ =>  
}
Run Code Online (Sandbox Code Playgroud)

遇到以下错误警告:warning: abstract type T in type pattern T is unchecked since it is eliminated by erasure

Dan*_*ral 47

这是由于类型擦除.除阵列外,JVM不知道任何类型参数.因此,Scala代码无法检查是否Option是一个 - Option[Int]或者Option[String]- 该信息已被删除.

您可以通过这种方式修复代码:

object Test {
  def test = {
    (Alice !? (100, "Hello")) match {
      case Some(i: Int) => println ("Int received "+i)
      case Some(s: String) => println ("String received "+s)
      case _ =>
    }
    (Alice !? (100, 1)) match {
      case Some(i: Int) => println ("Int received "+i)
      case Some(s: String) => println ("String received "+s)
      case _ =>
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这样你就不会测试它的类型Option,但它的内容类型是什么 - 假设有任何内容.A None将落入默认情况.


sep*_*p2k 8

有关类型参数的任何信息仅在编译时可用,而不是在运行时(这称为类型擦除).这意味着在运行时,Option[String]和之间没有区别Option[Int],因此类型上的任何模式匹配Option[String]也将匹配,Option[Int]因为在运行时两者都是正确的Option.

由于这几乎总是不符合您的意图,因此您会收到警告.避免警告的唯一方法是不要在运行时检查某些东西的泛型类型(这很好,因为它不会像你想要的那样工作).

没有办法检查a Option是一个Option[Int]还是Option[String]在运行时(除了检查内容,如果它是a Some).