The scala standard library seems to offer two methods for ensuring a recursive function does not cause a stack overflow.
One is the @tailrec
annotation, which supposedly causes the compiler to do something differently, or try extra hard to attempt tail recursion.
The other method is to actually change your function signature to return TailRec[T]
, and then use the TailCalls.done
and TailCalls.tailcall
functions to wrap your return values.
What is the difference? Is there any reason at all to use TailRec
when the compiler seems to be able to do it for me?