[Python-ideas] Tail recursion elimination
Andrew Barnert
abarnert at yahoo.com
Mon Jan 20 07:15:20 CET 2014
On Jan 19, 2014, at 17:49, Steven D'Aprano <steve at pearwood.info> wrote:
> But if TCE becomes opt-in, say by the proposed "return from" syntax,
> then you can keep your cake and eat it too. I can decide at *edit* time,
> "this function should have TCE enabled", and leave the rest of my code
> to have the "normal" behaviour.
My first post on the subject suggested adding a new keyword (I think I used "tailcall", borrowed from Guido's post) to do explicit tail calls, and only building TCE as an automatic optimization on top of it (which I'm pretty sure could be done with a trivial peephole optimizer rule) if you still need it after that. So obviously, I agree with this.
And yes, "return from" is definitely better than "tailcall"--readable and understandable, no new keyword, etc.
And I still think this would be a fun project even though I don't think I would ever use it. I tried effectively this same design against Stackless 2.6 a few years ago, but it sometimes leaked, and would crash whenever a C function called a Python function that tail called, and I ran out of free time to debug any further. The point is, this isn't a massive impossible project; many of the people insisting they want it are probably capable of writing it, even if they've never tried hacking on the interpreter. (The grammar is a huge pain the first time, however...)
More information about the Python-ideas
mailing list