Tra*_*own 24 syntax monads filtering scala for-comprehension
假设我有这个monadic类:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
以下是2.10.0 REPL会话:
scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a
res0: Foo[Int] = Foo(List(1))
Run Code Online (Sandbox Code Playgroud)
这与2.10.1中的情况相同:
scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a
Filtering!
res0: Foo[Int] = Foo(List(1))
Run Code Online (Sandbox Code Playgroud)
这对我来说是完全出乎意料的,并且在过滤需要额外约束(例如Scalaz \/或EitherT)的情况下会导致特别混乱的错误.
我无法在2.10.1发行说明中找到有关此更改的任何讨论.有人能指出这种新的贬低行为的引入位置和原因吗?
| 归档时间: |
|
| 查看次数: |
545 次 |
| 最近记录: |