3 ms·
Does Go have tail recursion? I'm guessing not since Go isn't big on the "optimization" part.
by jimmyed 6y ago
Does Go have tail recursion? I'm guessing not since Go isn't big on the "optimization" part.
- fredrikholm 6y ago> Go isn't big on the "optimization" part. Care to elaborate?
- jerf 6y agoIt's true. Go prioritizes compile speed over a ton of optimization. It does fast ones, but it doesn't do the expensive ones. I don't know whether the Go compiler never does tail-call optimization, but I do know it is never something you can count on. (There's two kinds of tail call optimization... there's the one where the compiler may choose to do it as an optimization step, but you can't ever count on it because at any time for any reason it may stop, so you have to assume it didn't happen. Then there's the kind you can absolutely count on, and program tail-call-based algorithms secure in the knowledge that they won't blow out the stack. I'm pretty sure Go doesn't have the latter.)
- fredrikholm 6y agoThank you for clarifying. Does this apply to procedural code as well? Go not implementing TCO makes sense as the language is _very_ Wirthian.
- thomascgalvin 6y agoYes: https://blog.usejournal.com/tail-recursion-in-go-fb5cf69a0f26 https://blog.usejournal.com/tail-recursion-in-go-fb5cf69a0f2...
- jerf 6y agoFWIW, I read "tail calls" as referring to the optimization that allows you to rely on the tail-recursive calls not generating stack frames, on the grounds that I'm not aware of any language that specifically forbids tail recursion.
- cratermoon 6y agoDepends on what you're optimizing for.
- monocasa 6y agoTail recursion isn't a computationally/memory intensive optimization to make, so it's been in since the beginning. It doesn't guarantee that it will make that optimization though, unlike some more functional languages that rely on that to generate the same stack consumption as a for loop would.