I have the following Scala code.
import scala.actors.Actor
object Alice extends Actor {
this.start
def act{
loop{
react {
case \"Hello\" =
This is due to type-erasure. The JVM does not know of any type parameter, except on arrays. Because of that, Scala code can't check whether an Option
is an Option[Int]
or an Option[String]
-- that information has been erased.
You could fix your code this way, though:
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 _ =>
}
}
}
This way you are not testing what the type of Option
is, but what the type of its contents are -- assuming there is any content. A None
will fall through to the default case.