Scala 2.10.1中新的减糖行为 [英] New desugaring behavior in Scala 2.10.1

查看:87
本文介绍了Scala 2.10.1中新的减糖行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个单子课程:

Suppose I have this monadic class:

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会话:

The following is from a 2.10.0 REPL session:

scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a
res0: Foo[Int] = Foo(List(1))

这与2.10.1中的内容相同:

And here's the same thing in 2.10.1:

scala> for { (a, b) <- Foo(List(1 -> "x")) } yield a
Filtering!
res0: Foo[Int] = Foo(List(1))

(对我来说)这完全是意外的,在过滤需要附加约束的情况下(例如Scalaz的 \/EitherT ).

This is completely unexpected (to me), and leads to particularly confusing errors in cases where filtering requires additional constraints (such as Scalaz's \/ or EitherT).

2.10.1发行说明中,我找不到有关此更改的任何讨论. .有人可以指出这种新的减糖行为在何处以及为什么被引入吗?

I wasn't able to find any discussion of this change in the 2.10.1 release notes. Can someone point out where and why this new desugaring behavior was introduced?

推荐答案

故事要复杂得多,实际上是在其中插入了2.10.0回归.

The story is more complex than that, and it's in fact a 2.10.0 regression that was plugged there.

c82ecab 中引入了"no- withFilter"行为,原因是 c82ecab href ="https://issues.scala-lang.org/browse/SI-6968"> SI-6968 ,此内容已部分恢复 SI-6646

The "no-withFilter" behavior was introduced in c82ecab, and because of things like SI-6968, this was reverted partially #1893. Further adaptations followed (SI-6646, SI-7183)

您要查找的外带句子是:

The takeaway sentence you're looking for is :

解析器无法假设模式(a,b)将匹配,结果 直到之后,才能静态知道.isInstanceOf [Tuple2]的 打字机.

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 2.10.1中新的减糖行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆