Skip to main content
 首页 » 编程设计

scala之Scala 2.10.1 中的新脱糖行为

2024年12月31日5zhengyun_ustc

假设我有这个 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) 
  } 
} 

以下来自 2.10.0 REPL session :
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 中找到有关此更改的任何讨论.有人可以指出这种新的脱糖行为是在哪里以及为什么被引入的吗?

请您参考如下方法:

故事比这更复杂,实际上是在那里插入了 2.10.0 回归。

“no- withFilter”行为是在 c82ecab 中引入的,并且因为诸如 SI-6968 之类的事情,这已部分恢复 #1893 .随后进行了进一步的调整( SI-6646SI-7183 )

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

The parser can't assume that a pattern (a, b) will match, as results of .isInstanceOf[Tuple2] can't be statically known until after the typer.