【问题标题】:New desugaring behavior in Scala 2.10.1Scala 2.10.1 中的新脱糖行为
【发布时间】:2013-06-29 18:31:39
【问题描述】:

假设我有这个单子类:

case class Foo[A](xs: List[A]) {
  def map[B](f: A => B) = Foo(xs map f)
  def flatMap[B](f: A => Foo[B]) = Foo(xs flatMap f.andThen(_.xs))
  def withFilter(p: A => Boolean) = {
    println("Filtering!")
    Foo(xs filter p)
  }
}

以下内容来自 2.10.0 REPL 会话:

scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a
res0: Foo[Int] = Foo(List(1))

在 2.10.1 中也是如此:

scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a
Filtering!
res0: Foo[Int] = Foo(List(1))

这完全出乎我的意料(对我而言),并且在过滤需要额外约束的情况下会导致特别令人困惑的错误(例如 Scalaz 的 \/ or EitherT)。

我在2.10.1 release notes 中找不到有关此更改的任何讨论。有人能指出这种新的脱糖行为是在哪里以及为什么引入的吗?

【问题讨论】:

    标签: scala syntax filtering monads for-comprehension


    【解决方案1】:

    故事比这更复杂,实际上是 2.10.0 的回归。

    “no-withFilter”行为是在c82ecab 中引入的,并且由于SI-6968 之类的原因,这部分被还原为#1893。随后进行了进一步的改编(SI-6646SI-7183

    你要找的外卖句子是:

    解析器不能假设模式 (a, b) 会匹配,作为结果 .isInstanceOf[Tuple2] 直到在 打字机。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多