Scala 异常在理解与类型注释



我试图理解在理解中处理空值和类型注释时似乎有什么奇怪的行为。

举个例子:

def f(): String = null
for {
  a <- Option("hello")
  b = f()
} yield (a, b)

预期结果:

//> res0: Option[(String, String)] = Some((hello,null)) 

但是,如果我向b类型添加类型注释

def f(): String = null
for {
  a <- Option("hello")
  b: String = f()
} yield (a, b)

然后我得到一个运行时异常:

//> scala.MatchError: (hello,null) (of class scala.Tuple2)

为什么会这样?无论如何,b不是第一个示例中隐式的 String 类型吗?第二个示例中的显式类型注释会更改什么?

(注意,示例在 Scala 2.11.4 中运行)

null 不是任何东西的实例:

scala> (null: String) match { case _: String => }
scala.MatchError: null
  ... 33 elided
scala> val s: String = null
s: String = null
scala> s.isInstanceOf[String]
res1: Boolean = false

http://www.scala-lang.org/files/archive/spec/2.11/08-pattern-matching.html#type-patterns

类型模式指定非空。

显示

翻译的一个技巧是注释显示:

scala> for {
     |   a <- Option("hello")
     |   b: String = f()
     | } yield (a, b) // show
object $read extends scala.AnyRef {
  def <init>() = {
    super.<init>;
    ()
  };
  object $iw extends scala.AnyRef {
    def <init>() = {
      super.<init>;
      ()
    };
    import $line4.$read.$iw.$iw.f;
    object $iw extends scala.AnyRef {
      def <init>() = {
        super.<init>;
        ()
      };
      val res1 = Option("hello").map(((a) => {
        val b: String = f;
        scala.Tuple2(a, b)
      })).map(((x$1) => x$1: @scala.unchecked match {
        case scala.Tuple2((a @ _), (b @ (_: String))) => scala.Tuple2(a, b)
      }))
    }
  }
}
scala.MatchError: (hello,null) (of class scala.Tuple2)
  at $anonfun$2.apply(<console>:10)
  at $anonfun$2.apply(<console>:10)
  at scala.Option.map(Option.scala:145)
  ... 39 elided

最新更新