TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What happened to proper tail calls in JavaScript? (2021)

130 pointsby mindBalmost 3 years ago

15 comments

blagiealmost 3 years ago
For the most part, programming constructs like these split the world into two camps:<p>- People who point out things can be done without them, who largely see them as useless due to lack of familiarity<p>- People who&#x27;ve used them, and see critical ways to restructure code to make it cleaner using said constructs<p>That&#x27;s been the case for a lot of progress in programming. Python, and ES2015, have done a wonderful job of bringing many previously-academic (including functional) programming constructs to a broader community, together with models like reactive programming.<p>That&#x27;s true of about half of the bits of progress in programming. Garbage collection was seen as garbage by C&#x2F;C++ programmers (&quot;What&#x27;s the big deal with adding a free() call? Stupid lazy people.&quot;). Garbage collections wasn&#x27;t useful because it omitted free() calls, but because it allowed the design of flows with many exit points (for example, different kinds of exception handling, exiting in the middle of a function, or giving up an object in a half-dozen places an hour later in code where tracking for free() requires building an ad-hoc reference counter or garbage collector).<p>The place where tail calls are a huge deal is when dealing with deep (or even infinitely deep) tree-like structures:<p><pre><code> if condition: return red(left_child) else: return blue(right_child) </code></pre> I don&#x27;t mind opt-in versus opt-out versus neither. All the reasons listed for not having them are dumb, though, and have good and easy work-arounds. The major one -- debugging -- it&#x27;s basically always good enough to just have a list of functions called, without having the whole tree. A 10,000 element stack trace is no help at all. You can, for example, keep the first 20 elements of the stack trace (don&#x27;t start PTCs unless the stack is of a certain depth), and then still keep a list of functions called:<p><pre><code> ipython webapp.main webapp.handler render.make_tree [PTC: render.red*91001, render.blue*10201] webapp.callback </code></pre> I have literally never seen a case where having a list of 100k calls in a stack traces is at all useful for anything.
评论 #31755888 未加载
评论 #31756151 未加载
评论 #31756690 未加载
评论 #31759435 未加载
评论 #31756673 未加载
评论 #31757484 未加载
thaynealmost 3 years ago
So, what happened to syntactic tail calls? I think that&#x27;s what I would prefer anyway, both because it makes it more clear from a debugging standpoint, since you opt in, and because you can get a warning (or compiler&#x2F;linter error if using a transpiler or linter) when your function isn&#x27;t actually tail recursive.
评论 #31754897 未加载
评论 #31758661 未加载
xg15almost 3 years ago
Reads like a sad state of affairs, but the article itself doesn&#x27;t really explain whatbthe actual concerns where that caused the proposals to be put on ice.<p>From reading, I mostly get &quot;PTC was un-implemented and put on ice because some browser vendors had issues with it; the alternative proposal, STC, was put on ice because other browser vendors had different issues with it. Then everyone (from the browser vendor side) kind of lost interest.&quot;<p>But what <i>were</i> the actual issues that blocked the two proposals?<p>Edit: Ah, I&#x27;m sorry. The issues with PTC are indeed described, but STC was brought forward specifically to address those reasons. So why wasn&#x27;t STC implemented then?
评论 #31753105 未加载
评论 #31758943 未加载
评论 #31753098 未加载
RcouF1uZ4gsCalmost 3 years ago
One advantage of syntactic tail calls is that you can give an error if you are unable to transform to a tail call.<p>Otherwise, you could have a program that seems to work file, and then you refactor and now your recursion isn’t a tail call anymore, and your stack blows up.
评论 #31754173 未加载
评论 #31755071 未加载
评论 #31753382 未加载
leohalmost 3 years ago
Worth reading why python doesn’t have it either<p><a href="http:&#x2F;&#x2F;neopythonic.blogspot.com&#x2F;2009&#x2F;04&#x2F;tail-recursion-elimination.html" rel="nofollow">http:&#x2F;&#x2F;neopythonic.blogspot.com&#x2F;2009&#x2F;04&#x2F;tail-recursion-elimi...</a>
评论 #31754455 未加载
评论 #31753880 未加载
评论 #31753650 未加载
评论 #31753402 未加载
评论 #31753296 未加载
erik_seabergalmost 3 years ago
We should switch on time travel debugging when it’s worthwhile, and be aware of its actual costs, rather than <i>always</i> paying for stack frames because they <i>might</i> serve as an incomplete history (missing loop iterations and calls that already returned).
dgb23almost 3 years ago
I use recursion in JS when implementing generic trees&#x2F;dags. I&#x27;m not worried at all about growing the stack because I use the language for UI stuff, where the depth of the trees is quite shallow and the data is small overall.<p>I don&#x27;t really know what the utility of TCO&#x2F;proper tail calls would be. You already have UX constraints that nudge to avoid having a ton of stuff on the screen.<p>As an example of where recursion of generic trees could be applied in a UI: Look at HN threads. Even exceptionally large threads have what, a couple hundred responses? With depth of maybe a dozen? Also you typically have affordances to navigate such a tree and only see the parts of it that you want. So it becomes even more trivially small.
评论 #31754614 未加载
评论 #31754321 未加载
评论 #31755947 未加载
评论 #31754434 未加载
kreetxalmost 3 years ago
The few real world discussions I&#x27;ve had about the topic of tail calls revealed that people mostly don&#x27;t know about them and are thus more &quot;afraid of the unknown&quot; rather than against the thing itself.
nsonhaalmost 3 years ago
&gt; Despite its inclusion in the 2015 language specification, PTC is currently only supported by Safari<p>Kind of related: I kept hearing about how backward Safari is, but is it really bad?<p>To me even as a web dev myself, the constant stream of new things pushed into modern browsers seem unsustainable. Saying no sometimes doesnt seem clearly bad, especially considering the fact that all browser vendors have some sort of agenda. Safari and Firefox are the only neutral ones but I&#x27;m not sure about the latter anymore.
k__almost 3 years ago
I had the impression, the ECMAScript spec would only accept proposals &quot;after&quot; they were implemented by the major players.<p>How did PTC sneak into the spec?
评论 #31753538 未加载
评论 #31755078 未加载
评论 #31753497 未加载
emilecantinalmost 3 years ago
I&#x27;ve seen a lot of chatter about tail calls, but I don&#x27;t think I&#x27;ve ever seen actual examples of what they look like. Does anyone use them or is it just something for language nerds to obsess about?
评论 #31753634 未加载
评论 #31753569 未加载
评论 #31759048 未加载
评论 #31753664 未加载
评论 #31753640 未加载
评论 #31753705 未加载
tomxoralmost 3 years ago
I&#x27;ve ended up writing a number of things in an equivalent iterative way due to this... which in retrospect feels like a positive thing because I find it far clearer.
评论 #31754225 未加载
adamddev1almost 3 years ago
Someone start a petition page. :-)
del_operatoralmost 3 years ago
Stack frames iirc
asciimovalmost 3 years ago
After thinking about this for a bit, the decision to avoid including this functionality is probably for the best.<p>Even though I would love for this feature to exist, I can see people unintentionally shooting themselves in the foot and not understanding why.<p>Often when you run up against call stack limitations, you actually need to reconsider the algorithm being used.<p>Trampolines can be used to bypass the call stack limitation. As an advanced technique, the majority of people having issues with a call stack problem will reconsider their solution before thinking about jumping on the trampoline.
评论 #31754988 未加载