News for hackers. Stuff that matters.
Re:How safe is Zig? by lmh on Thursday June 23, 2022 @01:24PM (#31852342)
Question for Zig experts:

Is it possible, in principle, to use comptime to obtain Rust-like safety? If this was a library, could it be extended to provide even stronger guarantees at compile time, as in a dependent type system used for formal verification?

Of course, this does not preclude a similar approach in Rust or C++ or other languages; but comptime's simplicity and generality seem like they might be beneficial here.

Re:How safe is Zig? by pron on Thursday June 23, 2022 @02:13PM (#31853053)
Not as it is (it would require mutating the type's "state"), but hypothetically, comptime could be made to support even more programmable types. But could doesn't mean should. Zig values language simplicity and explicitness above many other things.
Re:How safe is Zig? by lmh on Thursday June 23, 2022 @07:52PM (#31856485)
Thanks, that's informative. This was meant to clarify the bounds of Zig's design rather than as a research proposal. Otherwise, one might read it as an open invitation to just the sort of demonic meta-thinking that its users abhor.
Re:How safe is Zig? by kristoff_it on Thursday June 23, 2022 @04:52PM (#31855000)
Somebody implemented part of it in the past, but it was based on the ability to observe the order of execution of comptime blocks, which is going to be removed from the language (probably already is).

https://github.com/DutchGhost/zorrow

It's not a complete solution, among other things, because it only works if you use it to access variables, as the language has no way of forcing you.

Re:How safe is Zig? by lmh on Thursday June 23, 2022 @08:09PM (#31856577)
Thanks, that's interesting.
Re:How safe is Zig? by anonymoushn on Thursday June 23, 2022 @02:16PM (#31853095)
It is possible in principle to write a Rust compiler in comptime Zig, but the real answer is "no."
Re:How safe is Zig? by avgcorrection on Thursday June 23, 2022 @05:55PM (#31855618)
Why would the mere existence of some static-eval capability give you that affordance?

Researchers have been working on these three things for decades. Yes, “comptime” isn’t some Zig invention but a somewhat limited (and anachronistic to a degree) version of what researchers have added to research versions of ML and Ocaml. So can it implement all the static language goodies of Rust and give you dependent types? Sure, why not? After all, computer scientists never had the idea that you can evaluate values and types at compile-time. Now all those research papers about static programming language design will wither on their roots now that people can just use the simplicity and generality of `comptime` to prove programs correct.

Re:How safe is Zig? by ptato on Thursday June 23, 2022 @01:36PM (#31852515)
Not an expert by any means, but my gut says that it would be very cumbersome and not practical for general use.