Reputation: 255
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?
Upvotes: 1
Views: 275
Reputation: 48755
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.
Upvotes: 2