Hacker News new | ask | show | jobs
by lkey 2456 days ago
You picked that quote from the main page, not from the release notes, here is what directly follows:

* The reference implementation uses LLVM as a backend for state of the art optimizations.

* What other projects call "Link Time Optimization" Zig does automatically.

* For native targets, advanced CPU features are enabled (-march=native), thanks to the fact that Cross-compiling is a first-class use case.

* Carefully chosen undefined behavior. For example, in Zig both signed and unsigned integers have undefined behavior on overflow, contrasted to only signed integers in C. This facilitates optimizations that are not available in C. Zig directly exposes a SIMD vector type, making it easy to write portable vectorized code.

So the argument is "exact same compiler as C/C++, but more opportunities for optimization thanks to better semantics and better access to native instructions". This seems reasonable on its face, so care to elaborate on the doubt?

The arguments for not switching from C are often performance or target related, so a language that purports to be an alternative to C would want to point out that those issues aren't a problem.

The reasons to switch away from C are numerous and well documented.

1 comments

> For example, in Zig both signed and unsigned integers have undefined behavior on overflow, contrasted to only signed integers in C.

Huh, that's a strange choice. What's the safety story for Zig? Are these always undefined, or is that only in an "unchecked" build?

The latter. Zig actually kinda does define this behavior as "`a + b` shall not overflow" and inserts checks in debug and safe builds for it. To get overflow, which zig defines as wrapping overflow, you use a different operator and no check is inserted "`a +% b`". For speed optimized builds, unless you've explicitly told the compiler to insert checks in that scope, it will turn the checks into nops.

So, while it is technically correct to say that it has undefined behavior for overflow, the practical reality is quite different.

We do the same thing in Rust, but I think that characterizing this as UB is misleading, personally. We created a new category, "program error", for this, to distinguish from UB proper.

I'm not sure if Zig inherited the defined/implementation defined/undefined hierarchy from C and C++ though.

There's a task to rename UB to illegal behavior: https://github.com/ziglang/zig/issues/2402

As well as some followups to see if Zig can detect all of these errors: * https://github.com/ziglang/zig/issues/1966 * https://github.com/ziglang/zig/issues/2301

Undefined as in undefined by language spec. There are various processor implementations that have different results that are often quite useful. Would you preclude their use?
Right, the key here is that the behavior is defined. That’s why calling it “undefined behavior” is misleading.
It is defined if you want to restrict where your program runs.