Skip to content

Commit a659f92

Browse files
committed
first draft of recursion, thunks and trampolines
1 parent cb634a5 commit a659f92

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

_posts/2025-04-11-recursion-thunks-trampolines-with-java-and-scala.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ Instead of relying on the JVM, Scala implements Tail Call Optimisation at the co
160160
1. **Detection**: The Scala compiler (`scalac`) analyses the code to detect methods that make recursive calls to themselves in tail position. Remember, a tail position call is the absolute last action performed by the method before it returns.
161161
2. **Transform into Iteration**: Once the compiler has identified a self-recursive tail call, it rewrites the recursive method into an **iterative** one in the generated JVM bytecode.
162162
- Updates to the method's parameters (as if they were local variables).
163-
- A jump (like a _goto_ in bytecode terms) back to the beginning of the method's logic. This effectively creates a
164-
while loop structure within the bytecode.
163+
- A jump (like a _goto_ in bytecode terms) back to the beginning of the method's logic. This effectively creates a while loop structure within the bytecode.
165164
3. **Reuse Stack Frame**: By replacing the recursive call with parameter updates and a jump within the same method, no new stack frame is pushed onto the call stack for the "recursive" step. The method re-runs its logic with new values
166165
within the existing stack frame.
167166
4. **The `@tailrec` annotation**: Including the annotation tells the compiler to attempt to optimise the method for TCO. If it then fails because the method annotated is not direct self-recursion or in the tail position then it will raise an

0 commit comments

Comments
 (0)