代码之家  ›  专栏  ›  技术社区  ›  Jus12

scala模式与选项[any]匹配混乱

  •  31
  • Jus12  · 技术社区  · 14 年前

    我有以下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

    3 回复  |  直到 14 年前
        1
  •  43
  •   Daniel C. Sobral    14 年前

    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 _ =>
        }
      }
    }
    

    None

        2
  •  8
  •   sepp2k    14 年前

    Option[String] Option[Int] Option

    Some

        3
  •  2
  •   Kevin Wright    14 年前

    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 _ =>
        }
      }
    }