Difference in flattening an Option[List[Int]] in 2.9.1 and 2.10 nightly

前端 未结 1 1901
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 01:52

I get different behaviour in 2.9.1 and 2.10 nightly -- what changed?

Welcome to Scala version 2.9.1.final (OpenJDK Client VM, Java 1.6.0_22).
Type in express         


        
相关标签:
1条回答
  • 2020-12-19 02:44

    The reason is that Option acquired a flatten method in 2.10, that works only on nested Options.

    In 2.9, the call to flatten was added by an implicit conversion to Iterable, and the result was an Iterable (or a subtype thereof, depending on the nested value inside Option).

    Here's the signature of flatten in 2.10:

    def flatten[B](implicit ev: <:<[A, Option[B]): Option[B]
    

    It says: if you can find evidence that the element inside this option is an Option itself, say Option[B], I can flatten that and return an Option[B].

    Implicits are only tried if there is no method with that name, so that explains why it doesn't fall back to the 2.9 method.

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