问题
I looked at this question trying to better understand @tailrec
annotation in scala.
What I'm not sure is whether the annotation also hints the compiler to do some optimizations or it's only used for warnings when you mark a method that is not a tail-recursion?
More specifically - is this annotation might affect performance in any way? For example, if I don't put this annotation, the compiler will compile a tail-recursive function as non-tail recursive?
回答1:
As per the scaladoc:
A method annotation which verifies that the method will be compiled with tail call optimization.
If it is present, the compiler will issue an error if the method cannot be optimized into a loop.
This is a verification to trigger an error if you thought you wrote an optimizable function while you actually did not. Even if you don't put it, the compiler will optimize the code if it is possible.
来源:https://stackoverflow.com/questions/23546300/does-tailrec-affect-compiler-optimizations