recursion optimization in scala?
I'm building a scala compiler from the scala compiler source code by myself. In the source code of compiler, there are many tail-recursion functions/methods. Building scala compiler from its source code also requires compile the source code of compiler itself. If I add the option -g:notailcalls
to turn off tail-recursion optimization during compiling the source code, a statck overflow error will arise when running the built compiler.
In one word, is it possible that in a big and complex scala program which has many recursive calls, leaving out the tail-recursion optimization when compiling can cause stack overflow error at run time?
Certainly. But please note Scala is able to figure out if the function is tail recursive by itself, you don't need to pass the @tailrec
annotation to the function.
However, scala is unable to transform a suitable function to its tail-recursive form. You have to do that manually, but not every function can be transformed in that way.
链接地址: http://www.djcxy.com/p/80750.html上一篇: gcc:如果我在C ++中返回std :: string,是否没有尾递归?
下一篇: 在scala中递归优化?