2 ms·
> Tail calls are very simple: anything of the form 'return foo(...)'… It's a bit more complicated than that since there may be destructors (in C++, or C with c
by nybble41 6y ago
> Tail calls are very simple: anything of the form 'return foo(...)'…
It's a bit more complicated than that since there may be destructors (in C++, or C with certain extensions) or stack-frame cleanup which occur between 'foo' returning and the 'return' statement itself. Tail call elimination is only possible when the stack frame can be freed before the final function call; if the address of any local variable escapes the current function then it might not be an option even if the source otherwise appears tail-recursive. TCE is simpler in garbage-collected languages which allocate their stack frames from the heap (and typically lack precise destructors) since these cleanup operations can be left to the GC.