为了理解,我使用命令scala -Xprint:parser ForComprehensionDemo.scala
对其进行了简化。当我复制去糖的方法并调用它时,它会产生与理解不同的结果。知道为什么吗?
理解:
object ForComprehensionDemo {
def main(args: Array[String]): Unit = {
forWithIf(true)
}
def forWithIf(condition: Boolean) = {
val x = for {
a <- name(0)
b <- if (condition) {
name(1)
name(2)
} else {
name(100)
}
c <- name(2)
} yield {
a + b + c
}
println(x)
}
def name(x: Int): Option[String] = {
println("called for :" + x)
x match {
case 0 => Some(" aa ")
case 1 => Some(" bb ")
case 2 => Some(" cc ")
case _ => Some(" not identified ")
}
}
}
生成结果:
called for :0
called for :1
called for :2
called for :2
Some( aa cc cc )
去糖代码
def forWithIf(condition: Boolean) = {
val x = name(0).flatMap(((a) => if (condition)
{
name(1);
name(2)
}
else
name(100).flatMap(((b) => name(2).map(((c) => a.$plus(b).$plus(c)))))));
println(x)
};
生成结果:
called for :0
called for :1
called for :2
Some( cc )
只是漂亮打印机中的一个错误。它缺少if-else周围的parens。
一般来说,scala 2不能非常忠实地表示parens,但scala 3要好得多。
package desugar {
object ForComprehensionDemo extends scala.AnyRef {
def main(args: Array[String]): Unit = forWithIf(true);
def forWithIf(condition: Boolean) = {
val x = name(0).flatMap(((a) => (if (condition)
{
name(1);
name(2)
}
else
name(100)).flatMap(((b) => name(2).map(((c) => a.$plus(b).$plus(c)))))));
println(x)
};
def name(x: Int): Option[String] = {
println("called for :".$plus(x));
x match {
case 0 => Some(" aa ")
case 1 => Some(" bb ")
case 2 => Some(" cc ")
case _ => Some(" not identified ")
}
}
}
}
我很好奇Scala 3是怎么说的。-Xprint:all
在打字后说:
sugar.ForComprehensionDemo.name(0).flatMap[String](
{
def $anonfun(a: String): Option[String] =
(if condition then
{
sugar.ForComprehensionDemo.name(1)
sugar.ForComprehensionDemo.name(2)
}
else
{
sugar.ForComprehensionDemo.name(100)
}
).flatMap[String](
{
def $anonfun(b: String): Option[String] =
sugar.ForComprehensionDemo.name(2).map[String](
{
def $anonfun(c: String): String =
{
a.+(b).+(c)
}
closure($anonfun)
}
)
closure($anonfun)
}
)
closure($anonfun)
}
)
这在闭包中更难理解,但它有parens。在解析程序之后打印没有用处。
这里是IntelliJDesugar Scala code...
输出(FQN手动缩写(
def forWithIf(condition: Boolean) = {
val x = name(0)
.flatMap(((a) =>
(if (condition) {
name(1);
name(2)
} else {
name(100)
})
.flatMap(((b) =>
name(2)
.map(((c) => a.$plus(b).$plus(c)))))
));
println(x)
};
证实了somsnytt的回答。