加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala 2.10.1中的新的desugaring行为

发布时间:2020-12-16 09:47:42 所属栏目:安全 来源:网络整理
导读:假设我有这个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) }} 以下
假设我有这个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会话:

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找到任何讨论这个变化。有人可以指出在哪里和为什么这种新的desug行为被介绍?

解决方法

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

“no-withFilter”行为在c82ecab中引入,并且由于像SI-6968这样的事情,这部分恢复了#1893.进一步的适应(SI-6646,SI-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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读