【发布时间】: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