Kotlin recursion vs Java Recursion

I for one would not like an automatic tailrec optimization, because the optimization can be easily broken by even slight changes to the functions implementation. Suddenly the optimization would not be applied anymore at runtime. I like that Kotlin yields a compilation error when the function is declared tailrec without correct implementation.

The keyword serves as a documentation for my co-developers, too. They cannot accidentally break my tail recursion.

4 Likes