r/ProgrammingLanguages • u/gofl-zimbard-37 • Jan 18 '26
Why not tail recursion?
In the perennial discussions of recursion in various subreddits, people often point out that it can be dangerous if your language doesn't support tail recursion and you blow up your stack. As an FP guy, I'm used to tail recursion being the norm. So for languages that don't support it, what are the reasons? Does it introduce problems? Difficult to implement? Philosophical reasons? Interact badly with other feathers?
Why is it not more widely used in other than FP languages?
77
Upvotes
View all comments
Show parent comment
2
u/matthieum Jan 19 '26
I would note that, in general, there are many ways to "lose" context in a debugging session. For example, it's not unusual to only see the current value of a variable, and not the value the function was called with, because the variable has since been modified.
As such, TCE for a call to the same function is not much of a loss. The compiler could even sneak in a pseudo-variable to keep the depth.
TCE jumping between various functions, however, for example in a state-machine/parser implementation, would indeed not be super fun. You'd have no idea of the path taken to get where you are for any remotely interesting implementation.
But then again, if in exchange you get a guaranteed absence of stack overflow... it could be worth it. Especially if opt-in.
In Rust, the idea has (long) been to use a special keyword
becometo invoke a tail-call function, instead ofreturn. Not only does this make TCE opt-in, so you can opt-in only if you're willing to take the debuggability hit, but it also allows changing semantics:becomewhose destructor should be invoked.becomeis executed, not after.I personally prefer the former idea, at the moment, as the latter seems a bit too "gotcha" prone, but that may be because I'm just unused to TCE, having never used it (knowingly).