Hacker News new | ask | show | jobs
by Panzerschrek 5 days ago
> If you work in an environment where memory allocations can't fail or can't be handled if they fail, you might not want to use Zig,

It's most of environments. Basically any program running under a modern OS. So, why do this language exists, if its practical applicability is so small?

3 comments

This language exists so you can reuse the same code in environments where memory allocations may fail, and where memory allocations can't fail.

Let's say you write an application that runs as a Unix daemon in Zig. Later you may decide that your application is really the only thing you're interested in running on the target machine, and for performance and predictability reasons, you'd prefer to boot directly to your application, instead of to an OS that launches your daemon. You can just swap out the implementation of the std.Io runtime for one that targets the hardware directly, instead of a Unix. You don't have to make any changes to your application.

That's kind of an extreme case, but it's the kind of flexibility Zig provides.

> This language exists so you can reuse the same code in environments where memory allocations may fail, and where memory allocations can't fail.

In my hypothetical example of a language where allocation fails aren't exposed it's possible too. An allocation fail just triggers a full system reboot.

On modern OSs you can write Zig and just ignore allocation errors. It doesn't force you to handle them properly.

This language exists to supercede or supplement C, not JavaScript or C#.

It's practical applicability is similar to that of C, so I struggle to comprehend how it is "so small".

> On modern OSs you can write Zig and just ignore allocation errors.

I can ignore errors, but I still need to free memory manually if I want to avoid memory leaks. Languages like C++ or Rust have destructors, which do the job for me.

> This language exists to supercede or supplement C

There are way better alternatives, like Rust. Even C++ is better.

Zig has defer, your point is quite invalid.
defer can be forgotten to be written. C++ always calls destructors for local variables without additional programmer's intervention needed.
You don’t know there’s still millions of lines of code being written for environments with no OS or much more limited OS than what’s on modern desktops/laptops/servers?