3 ms·
Clojure is so close. I never understood why it went for a special form rather than proper TCO.
by mrtranscendence 6y ago
Clojure is so close. I never understood why it went for a special form rather than proper TCO.
- pimeys 6y agoIf I remember correctly it is a restriction of JVM. That's why Scala also needs a special annotation for TCO.
- patrickthebold 6y agoThat's not quite right. In Scala, the annotation is there so you get a complier error if it does not do the optimization. It doesn't affect when the compiler actually does the annotation. As you can imagine, there are cases where the developer is counting on the optimization but the code isn't such that it can be made.
- waynesonfire 6y agoYour post got me curious and I did some googling. I think this explains it, https://softwareengineering.stackexchange.com/questions/157684/what-limitations-does-the-jvm-impose-on-tail-call-optimization https://softwareengineering.stackexchange.com/questions/1576... Looks like it's a scalac optimization that doesn't always work hence the need for the annotation to give the developer feedback. There is also a link to a Closure discussion board exploring why Clojure doesn't have the annotation, by design. I didn't read it as I'm not that interested.
- iLemming 6y agoThis is what Rich said about TCO in Clojure in 2010: ```quote: When speaking about general TCO, we are not just talking about recursive self-calls, but also tail calls to other functions. Full TCO in the latter case is not possible on the JVM at present whilst preserving Java calling conventions (i.e without interpreting or inserting a trampoline etc). While making self tail-calls into jumps would be easy (after all, that's what recur does), doing so implicitly would create the wrong expectations for those coming from, e.g. Scheme, which has full TCO. So, instead we have an explicit recur construct. Essentially it boils down to the difference between a mere optimization and a semantic promise. Until I can make it a promise, I'd rather not have partial TCO. Some people even prefer 'recur' to the redundant restatement of the function name. In addition, recur can enforce tail-call position. ```endquote https://groups.google.com/g/clojure/c/4bSdsbperNE/m/tXdcmbiv4g0J https://groups.google.com/g/clojure/c/4bSdsbperNE/m/tXdcmbiv...