我有以下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 _ =>
}
}
}
执行Test.test
后,我得到输出:
scala> Test.test
Int received Some(Hi)
Int received Some(0)
我期待输出
String received Some(Hi)
Int received Some(0)
解释是什么?
作为第二个问题,我得到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
如何避免警告?
编辑:感谢您的建议。 Daniel的想法很好,但似乎不适用于泛型类型,如下例所示def test[T] = (Alice !? (100, "Hello")) match {
case Some(i: Int) => println ("Int received "+i)
case Some(t: T) => println ("T received ")
case _ =>
}
遇到以下错误警告:warning: abstract type T in type pattern T is unchecked since it is eliminated by erasure
答案 0 :(得分:43)
这是由于类型擦除。除阵列外,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 _ =>
}
}
}
这样你就不会测试Option
的类型,但它的内容类型是什么 - 假设有任何内容。 None
将落入默认情况。
答案 1 :(得分:8)
有关类型参数的任何信息仅在编译时可用,而不是在运行时(这称为类型擦除)。这意味着在运行时,Option[String]
和Option[Int]
之间没有区别,因此类型Option[String]
上的任何模式匹配也将匹配Option[Int]
,因为在运行时两者都只是{ {1}}。
由于这几乎总是不符合您的意图,因此您会收到警告。避免警告的唯一方法是不要在运行时检查某些东西的泛型类型(这很好,因为它无法像你想要的那样工作)。
无法在运行时检查Option
是Option
还是Option[Int]
(检查内容是否为Option[String]
除外)。
答案 2 :(得分:2)
如前所述,你在这里反对删除。
对于解决方案...... Scala actor为您可能发送的每种消息类型定义案例类是正常的:
case class MessageTypeA(s : String)
case class MessageTypeB(i : Int)
object Alice extends Actor {
this.start
def act{
loop{
react {
case "Hello" => sender ! MessageTypeA("Hi")
case i:Int => sender ! MessageTypeB(0)
}
}
}
}
object Test {
def test = {
(Alice !? (100, "Hello")) match {
case Some(MessageTypeB(i)) => println ("Int received "+i)
case Some(MessageTypeA(s)) => println ("String received "+s)
case _ =>
}
(Alice !? (100, 1)) match {
case Some(MessageTypeB(i)) => println ("Int received " + i)
case Some(MessageTypeA(s)) => println ("String received " + s)
case _ =>
}
}
}