Scala pattern matching confusion with Option[Any]

前端 未结 3 1259
天命终不由人
天命终不由人 2021-02-02 07:51

I have the following Scala code.

import scala.actors.Actor

object Alice extends Actor {
  this.start
  def act{
    loop{
      react {
        case \"Hello\" =         


        
相关标签:
3条回答
  • 2021-02-02 08:27

    As already stated, you're up against erasure here.

    For the solution... It's normal with Scala actor to define case classes for each message type you're likely to send:

    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 _ =>
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-02 08:29

    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.

    0 讨论(0)
  • 2021-02-02 08:31

    Any information about type parameters is only available at compile-time, not at runtime (this is known as type erasure). This means that at runtime there is no difference between Option[String] and Option[Int], so any pattern matching on the type Option[String], will also match Option[Int] because at runtime both is just Option.

    Since this is almost always not what you intend, you get a warning. The only way to avoid the warning is not to check the generic type of something at runtime (which is fine because that doesn't work like you want it to anyway).

    There is no way to check whether an Option is an Option[Int] or an Option[String] at runtime (other than inspecting the content if it's a Some).

    0 讨论(0)
提交回复
热议问题