4 ms·
This is what I appreciate about another aspect Zig, checked arithmetic, which is on by default in safe release builds. For example, if a u64 would overflow thr
by _vvhw 5y ago
This is what I appreciate about another aspect Zig, checked arithmetic, which is on by default in safe release builds.
For example, if a u64 would overflow through addition, then instead of just allowing the addition to wrap the u64 around to 0, Zig will rather detect this and crash at runtime with an error, unless you explicitly indicate through the "%+" operator that you do intend to allow the addition to wrap.
If this is an unknown unknown for you, you don't need to worry about it because Zig will detect it, simply because the default is correct.
Whereas in other languages that support something like this (and I think even Zig's checked arithmetic is still among the most extensive across all operators), you have to actually opt-in to checked arithmetic, it's not the default in safe builds, which always surprises me.