Discussion
Programming should be enjoyable
swiftcoder: This has always felt like the kind of thing we could be building compilers to solve. Recursive -> explicit stack is a very mechanical conversion, why can't we write a compiler transform pass that does that rewrite any time it detects a high potential for stack overflow?
Jaxan: Many compilers do change a recursion into a loop, avoiding the stack altogether!
Epa095: Yeah, tail call elimination, is definitely doable.Python famously does not have it because "Language inventor Guido van Rossum contended that stack traces are altered by tail-call elimination making debugging harder, and preferred that programmers use explicit iteration instead". https://en.wikipedia.org/wiki/Tail_call
jonathanlydall: It depends on whether the limited call stack capacity will be an issue for the particular problem you’re solving.I’m presently working on a problem which uses traversal of TypeScript file syntax trees.I can reasonably assume that we will never get a file with a deep enough syntax tree which would cause a stack overflow.A manually managed stack might seem safer, but as pointed out by this article the code would be more complicated and, in my case, for no good reason.