Comment

Comment by hgs3

> I really love Rust's error handling. Result forces you to address errors ...

> C's error handling story, in comparison, is straight up tragic. It seems to boil down to functions returning a "magic integer" like -1 or a null pointer to signal there was an error.

In C23 you can add the [[nodiscard]] attribute to the declaration of an enum or struct type, then at every call site of every function that returns said type, the C compiler will emit a warning if the result is unused. Instruct the compiler to treat warnings as errors and you can't ignore them.

Interestingly, Zig's error handling is conceptually C like, but limited to integer-like enums (no structs). It's basically a union of error integer code and success result. However, despite being limited to integers, Zig has dedicated compiler machinery for this so there's idiomatic consistency in Zig error handling.

parent hgs3 23 days ago

No comments yet

Be the first to share your thoughts.

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton