Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

AFAIK Zig is the only somewhat-big and known low-level language with TCO. Obviously, Haskell/Ocaml and the like support and it are decently fast too, but system programming languages they are not.


For guarantee:

https://crates.io/crates/tiny_tco

https://crates.io/crates/tco

As an optimization my understanding is that GCC and LLVM implement it so Rust, C, and C++ also have it implicitly as optimizations that may or may not apply to your code.

But yes, zig does have a formal language syntax for guaranteeing tail calls to happen at the language level (which I agree with as the right way to expose this optimization).


Zig's tco support is not much different than Clang's `[[clang::musttail]]` in C++. Both have the big restriction that the two functions involved are required to have the same signature.


> Both have the big restriction that the two functions involved are required to have the same signature.

I did not know that! But I am a bit confused, since I don't really program in either language. Where exactly in the documentation could I read more about this? Or see more examples?

The language reference for @call[0] was quite unhelpful for my untrained eye.

[0] https://ziglang.org/documentation/master/#call


Generally I also find Zig's documentation pretty lacking, instead I try looking for the relevant issues/prs. In this case I found comments on this issues [1] which seem to still hold true. That same issue also links to the relevant LLVM/Clang issue [2], and the same restriction is also being proposed for Rust [3]. This is were I first learned about it and prompted me to investigate whether Zig also suffers from the same issue.

[1]: https://github.com/ziglang/zig/issues/694#issuecomment-15674... [2]: https://github.com/llvm/llvm-project/issues/54964 [3]: https://github.com/rust-lang/rfcs/pull/3407


This limitation is to ensure that the two functions use the exact same calling convention (input & output registers, and values passed via stack). It can depend on the particular architecture.


C++:

> All current mainstream compilers perform tail call optimisation fairly well (and have done for more than a decade)

https://stackoverflow.com/questions/34125/which-if-any-c-com... (2008)


I couldn't actually figure out whether this TCO being done "fairly well" was a guarantee or simply like Rust (I am referring to the native support of the language, not what crates allow)


When that SO answer was written, it was not a guarantee.

You can now get a guarantee by using non-standard compiler attributes:

https://clang.llvm.org/docs/AttributeReference.html#musttail

https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html...


Depends on what you mean by "systems programming", you can definitely do that in OCaml.



I know of these. Almost added a disclaimer too -- that was not my point, as I am sure, you understand. Also Ocaml has a GC, unsuitable for many applications common to systems programming.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: