4 ms·
Doesn't Scala already do this?
by dahfizz 7y ago
Doesn't Scala already do this?
- smabie 7y agoYeah, but you need to explicitly annotate your functions with @rec.
- freekh 7y agoNot sure if this is accurate. You can enforce @tailrec if you want, but I believe it will try to in any case, even if it is not annotated.
- yashap 7y agoThis is correct. @tailrec will simply cause a compile failure if the compiler DOESN’T optimize the function, but the compiler does still optimize tail recursion without the annotation. It’s a weird but helpful annotation. For example, Scala won’t optimize methods that can be overridden (non-final and public/protected), which is easy to forget. So the annotation is a nice check/confirmation that the compiler is doing what you expect.