-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Tail Call Optimization (TCO) implementation in compile time #32743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Pretty sure this goes against one of those goals |
Yes, this part of the template has been deleted instead of filled out:
The second guideline is quite strikingly unmet here so I'd guess this suggestion will be declined, however desirable such a feature in JS might be. |
Oh, about the deletion, I tought this checklist was supposed to orient the author not to be really filled out. But I don't really see how it would change the runtime behavior of existing javascript code, because the behavior would be pretty much the same right? Could you explain how would it be a different behavior at runtime? |
Hmm, maybe we are interpreting that guideline differently. I think TS operates under the principle that if you are using syntax that works in the targeted version of JS, the output will be produced from the TS as-is. So while one could certainly argue that However, if I look at TypeScript's goals and non-goals, I see that non-goal #2 is:
So that applies here, right? |
Also:
|
I think this is out of scope. Tail call identification and rewriting can be done syntactically, so could be done at the same phase as minification, obfuscation, module tree shaking, etc., all of which we don't do. I believe the existing emit pipeline plugin support would allow for a sufficiently motivated person to plug this in if they really wanted it to happen at the same time as the rest of TypeScript compilation. Anyway 99.999% of JS devs are doing just fine without this feature and the "right" thing to do would be to lobby TC39 to put pressure on runtime authors to properly support something that's already in the ES spec. |
Proper tail call elimination [PCE] is part of the ECMAScript specification, so I don't see how a TypeScript-implementation would violate any of its goals. Furthermore, PCE is not a performance optimization - it's purpose is to prevent stack-growth and stack-overflows. |
Looking back at this, I think I originally wasn't interpreting this as a request for downleveling. Explicitly phrasing it in those terms, you get something like:
And then it being declined looks like
At least that's how I'm seeing it now. |
Search Terms
Functional Programming, ES6, Tail Call Optimization, TCO
Suggestion
Would be pretty nice to add a tail call optimization, once present in V8 for NodeJS 7.x, but later removed for some reasons I don't really understand, but about some other performance issues created in the browser. The idea is to create a transpiler to generate the iterative code for some tail-call optimizations, feature already present in Elm for example.
Use Cases
That would allow typescript people to take advantage of some other resources of pure functions without any real worry with StackOverflow from using the functional programming advantages.
The current approach in Typescript is not to use it, use iterative loops or even model the problem such a way that will never impact the code but is a huge problem for people trying to make the code more functional. There's already some implementation of it in some more functional transpilers for JS like Elm and is pretty useful in order to make functions modeling in an easier way.
Examples
Once a programmer code something like:
The code generated for javascript would be something like:
The text was updated successfully, but these errors were encountered: