在对象上定义递归方法:
object Recursive {
def recurse(maxDepth: Int = 10): Unit = {
if (maxDepth == 0) throw new Exception
recurse(maxDepth - 1)
}
}
给出:
scala> Recursive.recurse(10)
java.lang.Exception
at Recursive$.recurse(<console>:7)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.util.control.Exception$Catch.apply(Exception.scal...
但是在课堂上定义它:
class Recursive {
def recurse(maxDepth: Int = 10): Unit = {
if (maxDepth == 0) throw new Exception
recurse(maxDepth - 1)
}
}
给出:
scala> new Recursive recurse(10)
java.lang.Exception
at Recursive.recurse(<console>:7)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at Recursive.recurse(<console>:8)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcc..
方法完全相同。为什么在类上定义时不是尾递归?
答案 0 :(得分:16)
如果要执行尾递归,recurse
不能被覆盖。如果recurse
是可覆盖的,就像在class
声明中那样,其中的任何递归都必须使用动态方法调用(因为它可能是多态的),不能对goto样式的语句进行优化。 / p>
object
单例声明静态地确保明确的recurse调用,并让编译器继续进行尾递归优化。
答案 1 :(得分:10)
如果您希望方法是尾递归,则应使用@tailrec
对其进行注释。如果编译器无法应用TCO,则会出错。
scala> import annotation._
import annotation._
scala> class Recursive {
| @tailrec def recurse(maxDepth: Int = 10): Unit = {
| if (maxDepth == 0) throw new Exception
| recurse(maxDepth - 1)
| }
| }
<console>:12: error: could not optimize @tailrec annotated method: it is neither private nor final so can be overridden
@tailrec def recurse(maxDepth: Int = 10): Unit = {
^