3 ms·
zig looks like a 'better C', is there a list of something it does better than C(e.g. integer promotion,UB,etc), so that I should embrace it quickly and start to
by synergy20 4y ago
zig looks like a 'better C', is there a list of something it does better than C(e.g. integer promotion,UB,etc), so that I should embrace it quickly and start to use it in my embedded projects? would like to see a comparison table between zig vs c (or even c++)
- tarruda 4y agoI did a bit of Zig exploration a few months ago, here's a few things that caught my attention: - You don't have implicit allocations when using Zig stdlib. For example, when you instantiate an ArrayList or HashMap, you need to pass in an allocator, so you have full control over how memory is managed. So, even though you have higher level data structures, you still have a lot of low level control. - Very good error handling. IMO better than Rust and Golang, while still being very explicit about what is happening - "Uncolored" async functions, meaning there's no special syntax for declaring functions that can be paused/resumed. If I understood correctly (didn't try it a lot), you can turn any program into "async" by changing how I/O is handled globally. More details here: https://kristoff.it/blog/zig-colorblind-async-await/ https://kristoff.it/blog/zig-colorblind-async-await/
- kristoff_it 4y agoHere's a nice overview of Zig that should make even more sense if you know C: https://ziglang.org/learn/overview/ https://ziglang.org/learn/overview/
- flohofwoe 4y agoAs far as language features go: extensive comptime support, error handling and optionals integrated into the language, a new (to me at least) twist on generics, type reflection, and a couple of smaller 'convenience features' like type inference, if and switch being expressions, etc... Reading this from top to bottom gives a pretty good overview: https://ziglang.org/documentation/master/ https://ziglang.org/documentation/master/
- fileeditview 4y agoI would recommend this talk by the creator of Zig: https://www.youtube.com/watch?v=Gv2I7qTux7g&t=3021s https://www.youtube.com/watch?v=Gv2I7qTux7g&t=3021s He gives some examples of C vs Zig and what they tried to improve.
- defen 4y agoZig 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`