I'm trying to figure out what seems like weird behavior when handling zeros and type annotations inside an understanding.
As an example:
def f(): String = null for { a <- Option("hello") b = f() } yield (a, b)
leads to the expected:
//> res0: Option[(String, String)] = Some((hello,null))
however, if I add type annotation to type b
def f(): String = null for { a <- Option("hello") b: String = f() } yield (a, b)
then I get an exception at runtime:
//> scala.MatchError: (hello,null) (of class scala.Tuple2)
Why is this happening? Is b an implicit image of type String in the first example? What will change the explicit type annotation in the second example?
(Note: examples were run in Scala 2.11.4)
scala for-comprehension
Brian kent
source share