scala - Scala 2.10.1 中的新脱糖行为

标签 scala syntax filtering monads 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)
  }
}

以下来自 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.

关于scala - Scala 2.10.1 中的新脱糖行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17424763/

相关文章:

vector - Rust中的Vec <(i64,i64)>数据类型是什么?

log4j - 如何从 log4j 堆栈跟踪中过滤某些行?

css - 星号 CSS 规则错误

r - 使用列表中的两个按行参数分割/过滤数据框

html - Dojo//dijit.form.FilteringSelect//如何在Dojo的filter Select中的一个<select>字段中添加一个类

compiler-construction - 在这个 Scala 代码中 compare 与 compareTo 有何不同?

java - 读取 csv 文件时无法移动到下一行

mongodb - 如何以 ISO 格式而不是 Long [Play、Scala 和 ReactiveMongo] 格式在 MongoDB 中存储日期?

scala - 如何通过加入 Spark 来创建嵌套列?

rust - Rust函数语法问题,示例出现在nom中