7 ms·
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
by hyperbrainer 1y ago
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.
- vlovich123 1y agoFor guarantee: https://crates.io/crates/tiny_tco https://crates.io/crates/tiny_tco https://crates.io/crates/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).
- SkiFire13 1y agoZig'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.
- hyperbrainer 1y ago> 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 https://ziglang.org/documentation/master/#call
- SkiFire13 1y agoGenerally 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-1567447672 https://github.com/ziglang/zig/issues/694#issuecomment-15674... [2]: https://github.com/llvm/llvm-project/issues/54964 https://github.com/llvm/llvm-project/issues/54964 [3]: https://github.com/rust-lang/rfcs/pull/3407 https://github.com/rust-lang/rfcs/pull/3407
- ufo 1y agoThis 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.
- Thorrez 1y agoC++: > 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-compilers-do-tail-recursion-optimization https://stackoverflow.com/questions/34125/which-if-any-c-com... (2008)
- hyperbrainer 1y agoI 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)
- Thorrez 1y agoWhen 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://clang.llvm.org/docs/AttributeReference.html#musttail https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html#index-musttail-statement-attribute https://gcc.gnu.org/onlinedocs/gcc/Statement-Attributes.html...
- johnisgood 1y agoDepends on what you mean by "systems programming", you can definitely do that in OCaml.
- pjmlp 1y ago"Unix system programming in OCaml" https://ocaml.github.io/ocamlunix/ocamlunix.html https://ocaml.github.io/ocamlunix/ocamlunix.html MirageOS https://mirage.io/ https://mirage.io/ House OS, https://programatica.cs.pdx.edu/House/ https://programatica.cs.pdx.edu/House/ Just saying.
- hyperbrainer 1y agoI 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.