Scala 2.10.1中的新的desugaring行为

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发行说明中找到有关此更改的任何讨论.有人能指出这种新的贬低行为的引入位置和原因吗?

hui*_*ker 16

这个故事比这个更复杂,实际上是一个插入那里的2.10.0回归.

withFilterc82ecab中引入了"不 - "行为,并且由于像SI-6968这样的东西,部分地被还原了#1893.随后进行了进一步调整(SI-6646,SI-7183)

你正在寻找的外卖句子是:

解析器不能假设模式(a,b)匹配,因为.isInstanceOf [Tuple2]的结果直到typer之后才能被静态地知道.