假设我有这个单子类:
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 https://stackoverflow.com/q/17418165/334519).
我在 中找不到任何关于此更改的讨论2.10.1 发行说明 http://www.scala-lang.org/2.10.1。有人能指出这种新的脱糖行为是在哪里以及为什么引入的吗?
这个故事比这更复杂,它实际上是插入在那里的 2.10.0 回归。
The "no-withFilter
“ 行为被引入c82ecab https://github.com/scala/scala/commit/c82ecab,并且因为诸如此类的事情SI-6968 https://issues.scala-lang.org/browse/SI-6968,这已部分恢复#1893 https://github.com/scala/scala/pull/1893。随后进行了进一步的调整(SI-6646 https://issues.scala-lang.org/browse/SI-6646, SI-7183 https://issues.scala-lang.org/browse/SI-7183)
您正在寻找的要点是:
解析器不能假设模式 (a, b) 会匹配,因为结果
.isInstanceOf[Tuple2] 的直到之后才能静态地知道
打字机。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)