I have used and read about @tailrec
annotation to have a tail recursive method. I have gone through many links that explains it. For example it only works when for self-calling functions and shouldnot be overriden etc.
Everywhere it is mentioned that the compiler optimizes
. But what magic/concept does the compiler do to make it tail recursive. For a simple function below, what does the compiler do:
@tailrec def fact(acc: Int, n: Int): Int = {
if (n <= 1) acc
else fact(n * acc, n - 1)
}
fact(1,10)
I mean does it convert it into a loop where it repeatedly calls it and then returns the final value? Is there any link to paper which explains it