Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The strange thing is that the JVM can handle them because Scala and Kotlin have them. Java though, doesn't.


Scala and Kotlin just implement them using a trampoline, the JVM does not itself support tail calls.


How do they do that? I thought it would need to be done at the JVM level so that stack didn't grow.


Every recursion can be converted to code that uses only a single stack. Tail calls can be easily eliminated at compile time automatically as well, that’s why it only needs compile-time support — I don’t specifically know what Scala/etc do, but the mentioned trampoline is basically just a function pointer one can jump to, accumulating results in some non-stack data structure if needed.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: