News for hackers. Stuff that matters.
Re:Zig 0.12.0 Release Notes by latch on Saturday April 20, 2024 @08:06AM (#40096732)
I've written a lot of Zig, including a somewhat popular http framework, a driver for PostgreSQL and one for DuckDB.

Personally, I feel that if you want to grow as a programmer, no matter what you're doing, there's little that offers more bang for your buck than learning C. You can pick up C relatively quickly, and that knowledge will serve you (maybe mostly indirectly) throughout your entire career.

Learning Zig is a much better option. Too many quality of life improvements over C, and you still learn the same fundamentals. If you're interested, I wrote something that might help (1).

As for _using_ Zig. I think it's a good idea to take a conservative view and assume the ecosystem is hostile. The quality of test coverage of the standard library varies. Breaking changes are frequent and aren't always trivial. 3rd party libraries are often abandoned and many are written by people learning Zig (including my own!). It's really great for learning/side/fun project because you can get distracted for weeks writing something you never intended to.

It's not actively hostile, but I think approaching it that way will help manage expectations.

I would strongly consider tracking the master branch. If you write anything substantial in Zig, you will 100% run into a must-have bug-fix or feature in the stdlib or 3rd party library that depends on on 0.13.x development commit.

(1) - https://www.openmymind.net/learning_zig/

Re:Zig 0.12.0 Release Notes by RetroTechie on Saturday April 20, 2024 @08:44AM (#40096961)
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.

Re:Zig 0.12.0 Release Notes by flohofwoe on Saturday April 20, 2024 @08:55AM (#40097035)
IMHO (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.

Re:Zig 0.12.0 Release Notes by latch on Saturday April 20, 2024 @09:31AM (#40097244)
To 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.

Re:Zig 0.12.0 Release Notes by throwawaymaths on Saturday April 20, 2024 @01:19PM (#40098900)
Even 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

I'm not even sure if the faq is in jest or not.

Re:Zig 0.12.0 Release Notes by flohofwoe on Saturday April 20, 2024 @02:12PM (#40099450)
This "spiral type notation" has been acknowledged in K&R 2nd Edition as one of the less fortunate design decisions in C.
Re:Zig 0.12.0 Release Notes by pjmlp on Monday April 22, 2024 @09:51AM (#40114366)
Ironically, almost 40 years later Pascal linage of languages finally have their revenge in type declarations.
Re:Zig 0.12.0 Release Notes by jstimpfle on Monday April 22, 2024 @10:25AM (#40114695)
The 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.

Re:Zig 0.12.0 Release Notes by throwawaymaths on Monday April 22, 2024 @01:47PM (#40116779)
That's actually a problem. In c, variable declarations are not context-free.

    mytype *foo;
Could be a variable declaration, Or is it?

Suppose you had

    int mytype = 42;
    int foo = 1;
Before it. The C compiler will happily turn the statement into a multiplication with the result going to /dev/null

    var foo: mytype;
Can never be parsed with any other meaning depending on how the chips fall after parsing (and possibly analysis, given that macros are a thing) the entirety of the file.
Re:Zig 0.12.0 Release Notes by jstimpfle on Monday April 22, 2024 @02:17PM (#40117106)
Yes it comes with its problems. The context sensitivity is annoying and it makes creating good tooling hard. But given macros it doesn't worsen the situation much anyway.

For writing a batch parser/compiler that properly reads all the includes too, the context sensitivity is only a theoretical problem. Adding a type table is easy.

Not having to type "var" all the time is very nice on the other hand.

Re:Zig 0.12.0 Release Notes by throwawaymaths on Monday April 22, 2024 @10:16PM (#40127767)
Clarity is always better than saving keystrokes. Code is generally more read than written.
Re:Zig 0.12.0 Release Notes by jstimpfle on Tuesday April 23, 2024 @04:02AM (#40129619)
Adding keystrokes is not the same as adding clarity, though.
Re:Zig 0.12.0 Release Notes by throwawaymaths on Saturday April 20, 2024 @01:22PM (#40098930)
Zig 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.
Re:Zig 0.12.0 Release Notes by pjmlp on Monday April 22, 2024 @09:47AM (#40114327)
Having 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.

Re:Zig 0.12.0 Release Notes by pjmlp on Monday April 22, 2024 @09:50AM (#40114358)
I 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).