News for hackers. Stuff that matters.
Re:Tail Call Recursion in Java with ASM (2023) by 1932812267 on Sunday March 30, 2025 @12:25PM (#43525333)
This isn't a _general_ tail call optimization--just tail recursion. The issue is that this won't support mutual tail recursion.

e.g.:

(defun func-a (x) (func-b (- x 34)) (defun func-b (x) (cond ((<= 0 x) x) ('t (func-a (-x 3))))

Because func-a and func-b are different (JVM) functions, you'd need an inter-procedural goto (i.e. a tail call) in order to natively implement this.

As an alternative, some implementations will use a trampoline. func-a and func-b return a _value_ which says what function to call (and what arguments) for the next step of the computation. The trampoline then calls the appropriate function. Because func-a and func-b _return_ instead of actually calling their sibling, the stack depth is always constant, and the trampoline takes care of the dispatch.

Re:Tail Call Recursion in Java with ASM (2023) by knome on Sunday March 30, 2025 @03:32PM (#43526835)
Sounds like a manual form of clojures recur function.

https://clojuredocs.org/clojure.core/recur

Re:Tail Call Recursion in Java with ASM (2023) by 1932812267 on Sunday March 30, 2025 @03:49PM (#43526996)
Clojure's loop/recur is specifically tail recursion like scala's tailrec or the optimization described in the blogpost. It doesn't use trampolines to enable tail calls that aren't tail recursion.