| ▲ | tialaramex a day ago | ||||||||||||||||
Forbidding recursion is pretty annoying. One of the nice things that's on the distant horizon for Rust is an explicit tail recursion operator perhaps named `become`. Unlike naive recursion, which as this video (I haven't followed the link but I'm assuming it is Laurie's recent video) explains risks stack overflow, optimized tail recursion doesn't grow the stack. The idea of `become` is to signal "I believe this can be tail recursive" and then the compiler is either going to agree and deliver the optimized machine code, or disagree and your program won't compile, so in neither case have you introduced a stack overflow. Rust's Drop mechanism throws a small spanner into this, in principle if every function foo makes a Goose, and then in most cases calls foo again, we shouldn't Drop each Goose until the functions return, which is too late, that's now our tail instead of the call. So the `become` feature AIUI will spot this, and Drop that Goose early (or refuse to compile) to support the optimization. | |||||||||||||||||
| ▲ | tgv a day ago | parent | next [-] | ||||||||||||||||
In C, tail recursion is a fairly simple rewrite. I can't think of any complications. But ... that rewrite can increase the cyclomatic complexity of the code on which they have some hard limits, so perhaps that's why it isn't allowed? And the stack overflow, of course. | |||||||||||||||||
| |||||||||||||||||
| ▲ | morshu9001 12 hours ago | parent | prev | next [-] | ||||||||||||||||
Thinking recursively is one thing, but can't remember the last time I've wanted to use recursion in real code. | |||||||||||||||||
| ▲ | zozbot234 a day ago | parent | prev [-] | ||||||||||||||||
The tail recursion operator is a nice idea, but the extra `become` keyword is annoying. I think the syntax should be `return as`: it uses existing keywords, is unambiguous and starts with `return` which tail recursion is a special case of. | |||||||||||||||||
| |||||||||||||||||