A direct link extends to the definition of value forms.

Similar problems 1. and 2. indicate real cross-reference problems in scala. But I found this simple case when there are no direct links. All units are perfectly autonomous.

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 } 

The solutions to solve this problem found so far are:

  • Make shapes a lazy val (as in 2. )
  • Rename the recursive function to the non- recursiveMethod prefix.
  • Delete optional parameter j: Int = 3
  • Do not have recursiveMethod the call itself

Can someone explain to me why these are solutions, although they seem to be completely unrelated to the problem?

+7
reference scala
source share
1 answer

With -Xprint:typer you can see the synthetic, which is listed with a direct link:

 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 = () } } 

There are at least a few tickets related to creating methods close to their origin in order to avoid these kinds of invisible problems.

Update: for pririent:

Oldie but goodie

Closer to the heart, if not directly related to this problem

I especially liked the "rename function so that it is not a prefix of another function."

This shows how renaming reorders participants:

  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; () }; () } }; 

Needless to add, it should be a mistake or regression. Correctly?

Update:

Indeed, the sorting test is syntName.toString.startsWith , which explains why renaming another function matters. This shows how a fragile name distorts all errors. He likes termites that come out of the tree so often, reminding you that over the past five years they have compromised the structural integrity of the frame.

This is the code with the comment [Martin] This is pretty ugly. So he is not unknown, he just needs someone with free time.

+5
source share

All Articles