Scala 正向引用扩展了值形状的定义
类似的问题,并提到scala中前向引用的实际问题。 但是我发现了一个特别简单的例子,没有正向引用。所有模块都是独立的Scala 正向引用扩展了值形状的定义,scala,reference,Scala,Reference,类似的问题,并提到scala中前向引用的实际问题。 但是我发现了一个特别简单的例子,没有正向引用。所有模块都是独立的 def testFunction() = { def recursiveMethod(i: Int, j: Int = 3): Unit = i match { case 0 => println(s"finished") case i => recursiveMethod(i-1) } val shapes = List[String](
def testFunction() = {
def recursiveMethod(i: Int, j: Int = 3): Unit = i match {
case 0 => println(s"finished")
case i => recursiveMethod(i-1)
}
val shapes = List[String]()
def recursive(i: Int): Unit = i
}
迄今为止发现的克服该问题的解决方案:
- 使
成为惰性val(如中所示)形状
- 将函数
重命名为不是recursive
recursiveMethod
- 删除可选参数j:Int=3
- 没有调用自身的递归方法
有人能给我解释一下为什么这些是解决方案,尽管它们似乎与问题完全无关吗?使用
-Xprint:typer
可以看到正向引用的合成:
package oops {
object Test extends scala.AnyRef {
def <init>(): oops.Test.type = {
Test.super.<init>();
()
};
def testFunction(): Unit = {
def recursiveMethod(i: Int, j: Int = 3): Unit = i match {
case 0 => scala.this.Predef.println(scala.StringContext.apply("finished").s())
case (i @ _) => recursiveMethod(i.-(1), recursiveMethod$default$2)
};
val shapes: List[String] = immutable.this.Nil;
def recursive(i: Int): Unit = {
i;
()
};
<synthetic> def recursiveMethod$default$2: Int @scala.annotation.unchecked.uncheckedVariance = 3;
()
};
def main(args: Array[String]): Unit = ()
}
}
包oops{
对象测试扩展了scala.AnyRef{
def():oops.Test.type={
测试。超级。();
()
};
def testFunction():单位={
def recursiveMethod(i:Int,j:Int=3):单位=i匹配{
案例0=>scala.this.Predef.println(scala.StringContext.apply(“finished”).s())
案例(i@)=>recursiveMethod(i.-(1),recursiveMethod$default$2)
};
val shapes:List[String]=immutable.this.Nil;
def递归(i:Int):单位={
我
()
};
def recursiveMethod$default$2:Int@scala.annotation.unchecked.uncheckedVariance=3;
()
};
def main(args:Array[String]):Unit=()
}
}
为了避免这些看不见的问题,至少有两个票证与生成接近其原点的方法有关
更新:对于色情狂:
我特别喜欢“重命名函数,使其不是其他函数的前缀”
这显示了重命名如何重新排序成员:
abstract trait Oops extends scala.AnyRef {
def /*Oops*/$init$(): Unit = {
()
};
def testFunction(): Unit = {
def recursiveMethod(i: Int, j: Int = 3): Unit = i match {
case 0 => scala.this.Predef.println(scala.StringContext.apply("finished").s())
case (i @ _) => recursiveMethod(i.-(1), recursiveMethod$default$2)
};
<synthetic> def recursiveMethod$default$2: Int @scala.annotation.unchecked.uncheckedVariance = 3;
val shapes: List[String] = immutable.this.Nil;
def xrecursive(i: Int): Unit = {
i;
()
};
()
}
};
abstract trait Oops扩展了scala.AnyRef{
def/*Oops*/$init$():单位={
()
};
def testFunction():单位={
def recursiveMethod(i:Int,j:Int=3):单位=i匹配{
案例0=>scala.this.Predef.println(scala.StringContext.apply(“finished”).s())
案例(i@)=>recursiveMethod(i.-(1),recursiveMethod$default$2)
};
def recursiveMethod$default$2:Int@scala.annotation.unchecked.uncheckedVariance=3;
val shapes:List[String]=immutable.this.Nil;
def xrecursive(i:Int):单位={
我
()
};
()
}
};
无需添加,这一定是一个bug或回归。对吧?
更新:
实际上,排序测试是syntName.toString.startsWith
,这解释了为什么重命名另一个函数会有所不同。这显示了脆弱的名称混乱是如何到处泄漏bug的。这就像有白蚁,它们不时地从木制品中冒出来,提醒你在过去的五年里,它们已经破坏了框架的结构完整性
这是带有注释的代码。[Martin]这很难看。所以它不是未知的,只是需要有空闲时间的人。是的,我认为这是一个bug或回归。因为即使在预编译的代码中,我也看不到任何快进。@MikaëlMayer更新为丑陋的。