3 ms·
Zig things that I miss when I have to go back to C: - All integer operations trap on overflow in safe build modes; with explicit operators for saturating or wr
by defen 4y ago
Zig things that I miss when I have to go back to C:
- All integer operations trap on overflow in safe build modes; with explicit operators for saturating or wrapping arithmetic
- No implicit integer promotion unless the destination type can represent all values of the source type (so no implicit signed/unsigned conversions unless they're statically guaranteed to be safe - e.g. a u8 can coerce to an i32 but not to an i8)
- Arbitrary bit-size integers (C23 will have this)
- Enums that are actually useful and fun, vs the complete waste of time that C's enums are (Enum values are namespaced, you can't directly use their values as integers, you can control the underlying representation if you want, etc)
- Built-in support for tagged unions, also known as sum types (bare union + a tag indicating which field is active)
- Safe unions in safe build modes (compiler inserts a hidden tag to track which field is active)
- A standard library that's actually useful (it's small compared to some other languages, and not well-documented yet, but it's not littered with landmines the way C's is)
- A modern import system instead of preprocessor-style copy/pasting text
- Compile-time programming in Zig, instead of preprocessor macros
- Arrays are an actual type, instead of decaying to pointers
- Much better support for pointers (pointer + length AKA slices are the primary way to deal with multiple items whose length can vary at runtime; also single-item pointers and multi-item pointers are different types, so you can't accidentally index into a single-item pointer or attempt to dereference a multi-item pointer without providing an index)
- Errors must be handled, with convenient syntax for passing the error up the stack + inferred error sets so that you don't have to explicitly annotate the set of possible errors for most functions
- Nulls encoded in the type system so that they must be explicitly handled.
- test blocks for writing tests inline and running them with `zig test`