3 ms·
Zig is positioned as a sort of "better C", correct? With that in mind, how does Zig compare to C, complexity-wise? Both for learning, and its implementation.
by RetroTechie 3y ago
Zig is positioned as a sort of "better C", correct?
With that in mind, how does Zig compare to C, complexity-wise? Both for learning, and its implementation.
Is it a 'bigger' language than C? Just different? Easier to grasp? Or takes more effort to get a good understanding of? About the same as C?
Anyway: a very promising project. Nice to see it progressing towards a mature ecosystem.
- flohofwoe 3y agoIMHO (not the parent): from the perspective of a C programmer, Zig is several things: 1. A "better C" in the sense that the language core of Zig fixes the commonly known and accepted design warts of C which are hard to fix because of backward compatibility requirements - this part is actually less complex and easier to learn than C because there's less implicit and less unexpected hidden behaviour while having roughly the same "language surface". If you are used to C's somewhat sloppy "hippie-approach" to programming, be prepared that the Zig compiler will yell at you a lot at the start of the learning phase, especially when it comes to implicit type conversions which follow very strict rules in Zig (when compared to C). 2. A much richer and actually useful stdlib (in this area Zig is also a much better C++), don't quite expect a Python-style "batteries included" stdlib though. 3. A much more complete compiler toolchain package, with a build system, package manager, C/C++/ObjC integration and cross-compilation to the important target platforms included and working out of the box (and most of that integrated into the Zig executable, a vanilla Zig install actually only has a single executable, the Zig compiler). 4. And finally the "visionary part", where Zig differs most from C and other languages and explores its own path, like the approach to comptime, generics, type reflection and error handling.
- latch 3y agoTo add more concrete examples, you could look at tagged unions, errorsets and optionals. An `if` statement in Zig has a few different faces, traditional condition, unwrapping an optional and unwrapping an errorset. That makes the language "bigger", but these are things you'll regularly face in C anyways, even with trivial examples. I think most of the things Zig (purely as a language) has "added" to be labeled a "Better C" are all pragmatic and don't increase any cognitive load. The other thing worth mentioning is that Zig has a number of safety advantages. I just touched up my DuckDB driver, and their C API exposes the underlying storage for a column as a (void ), the format of which depends on the column type. In Zig, I can map this to a typed tagged union. For an bigint column, instead of a (void ), I end up with an []i64.
- throwawaymaths 3y agoEven if you never use zig generics --if nothing else eliminating fucking spiral types is enough of a quality of life improvement alone that you should consider zig over C. if you've never heard of them: https://c-faq.com/decl/spiral.anderson.html https://c-faq.com/decl/spiral.anderson.html I'm not even sure if the faq is in jest or not.
- flohofwoe 3y agoThis "spiral type notation" has been acknowledged in K&R 2nd Edition as one of the less fortunate design decisions in C.
- pjmlp 2y agoIronically, almost 40 years later Pascal linage of languages finally have their revenge in type declarations.
- jstimpfle 2y agoThe clockwise/spiral rule is a misunderstanding, it doesn't exist and C type syntax is not that complicated. The thing that isn't well known about C type syntax is that there is no type syntax. At least originally, there wasn't really type syntax. (This was later muddied by e.g. types in function signature parameters, which came from C++ -- and which are a good thing to be clear). Types follow usage (given as normal code expressions), and if you can read normal expressions, you'll know what a type declaration means. The syntax is simply: type-name EXPR. where type-name is a simple name (or tagged name, such as struct mystruct), and EXPR is a limited subset of normal expressions. Example: int *foo[10]; Read like (type-name = int), (EXPR = *foo[10]). Now, if you now how to understand the EXPR, you'll know what the type of the variable foo is. Since (*foo[10]) must be int, you can simply go backward to know the "shape" of foo. Similarly, there is some elegance how to define a type name instead of a variable -- you simply add the "typedef" keyword and that's it. Even though that idea is a bit arcance admittedly, and not so pure anymore today, now I'm used to the C way of declaring types, and I think I like the fact that there isn't much of an additional syntax. Maybe it even removes mental overhead when reading code.
- 2y ago
- throwawaymaths 3y agoZig elminates the pointer/array ambiguity issue that exists in C. It does increase the complexity of the language, but the safety you get out of it is very much worth it.
- pjmlp 2y agoHaving learn systems in BASIC variants, Pascal dialects and Z80/68000/8086 before dealing with C, I always prefered that added language complexity that has saved me so many times when not using C. Pity that even Dennis Ritchie failed to convince WG14 about fat pointers.
- pjmlp 2y agoI still need a couple of things to win over me, a proper way to deal with use-after-free that isn't already what we have since other safe systems languages like Modula-2, and what probaly never happen due to FOSS beliefs among the community, Zig binary libs (not C bindings with double effort with Zig on both sides).