Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 正向引用扩展了值形状的定义_Scala_Reference - Fatal编程技术网

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](

类似的问题,并提到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 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更新为丑陋的。