Why is there a difference in behavior between these two pattern matches in a for comprehension?

后端 未结 2 1639
暗喜
暗喜 2020-12-11 08:01

Consider this Map[String, Any]:

val m1 = Map((\"k1\" -> \"v1\"), (\"k2\" -> 10))

Now let\'s write a for:

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 08:18

    Well, the latter example doesn't work because it isn't spec'ed to. There's some discussion as to what would be the reasonable behavior. Personally, I'd expect it to work just like you. The thing is that:

    val v: String = (10: Any) // is a compile error
    (10: Any) match {
        case v: String =>
    } // throws an exception
    

    If you are not convinced by this, join the club. :-) Here's a workaround:

    for (va @ (v: String) <- l1) println(v)
    

    Note that in Scala 3, you can:

    for (case v: String <- l1) println(v)
    

提交回复
热议问题