News for hackers. Stuff that matters.
Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @04:54AM (#25620793)
Consider the domain Zig targets -- low-level programming -- and other languages targeting it: C, C++, Ada and Rust. The first is very simple but severely lacks in both expressive power and safety, and the other three would all easily make any list of the top five most complex programming languages in the history of software; they're byzantine temples for those who worship at the altar of accidental complexity.

Zig offers a radical design. It seeks to be as simple as C — which is particularly important in low-level programming for both portability [1] and humans and machine analysis — while also giving you safety and high expressivity. Obviously, radical design means doing things differently.

Zig rejects any kind of implicitness; all calls must appear as calls. This means no operator overloading, no destructors, and no magical traits. It does so, however, without giving up on expressivity by using other mechanisms.

All low-level programming languages suffer from low abstraction. That comes with the territory. By that I mean that implementation details (especially around memory management) are hard to hide. Changing those details in some component often require changing the clients of that component as well. But C++ and Rust have a design philosophy that tries to hide this intrinsic quality of low-level programming by making the final code appear on the page as if it has abstraction capabilities similar to high-level languages. Of course, that illusion breaks as soon as you try to change code, and it's debatable whether this design helps or harms program comprehension. Zig rejects the C++ philosophy in favour of a brand new, and pretty revolutionary one, that reimagines how low-level programming is done. Unlike Rust, Zig isn't a cleaned-up C++ with a few extra tricks, but it isn't a new C, either. It's something completely different.

You might prefer Zig's design for this domain (as I do) or you might prefer the more traditional C++ design, but the great thing is that Zig is so simple and easy to learn that it's easy to try and find out.

[1]: Consider what it would take to port C++ (or a C++-like language) to new microcontroller you build. You would either need to port LLVM or an extremely complex compiler — a huge undertaking. This is why C (well, there's a tiny bit of Ada, too) still reigns supreme even in domains where correctness is of utmost importance: https://youtu.be/uqan23518Yc?t=632

Re:Zig in 30 Minutes by eklavya on Sunday January 03, 2021 @07:13AM (#25621336)
What would be accidentally complex about Rust? Sure, if you don't provide the static guarantees that Rust does (and only Rust provides AFAIK), you can be a lot simpler :)

The more I use Rust the more I am convinced I can write high level performant code. I mean it's ridiculous that sometimes I forget I am writing Rust and not Scala!!

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @08:13AM (#25621581)
Rust, like C++ if not more, elevates certain technical details like lifetime, static vs. dynamic dispatch, partial evaluation, and sequential composition ("default" vs. async) to multiple layers of syntactic constructs (types, generics, macros). That focuses the language on accidental complexity, although some people do find that C++ design philosophy appealing (I would expect some high correlation between people who like Scala and people who like Rust as I would between those who don't like either). Of course, it's a matter of personal aesthetics, but I think that style is misapplied for low-level domains, putting a lot of effort into a veneer of high abstraction without actually providing it (you still have to think about these aspects, as you do in all low-level languages, and, much more importantly, it still requires changing client code when details change, as in all low-level languages -- i.e. changing a Rust program is not like changing a Scala program), doing, IMO, more harm than good.

As someone who does a lot of low-level development, before Zig I didn't think any approach other than that of C or that of C++ was even possible. I don't know if Zig will fulfil its promise, but its radical design, that's unlike anything we've ever seen, sure offers a fresh perspective on things.

As to static guarantees -- they are never the point; they are a means to an end: writing more correct programs. That goal is just as valued by Zig as it is by Rust, Zig just tries to achieve it differently. Which achieves it better? It's hard to know right now. I have little doubt that Rust could make things that are today written in C++ safer, but I have serious doubts whether it would be adopted in truly safety-critical domains, where C is king. I think Zig could be.

Re:Zig in 30 Minutes by eklavya on Sunday January 03, 2021 @09:02AM (#25621805)
Thanks for your insights in the matter. I have tried writing a VM with Rust (crafting interpreters) but outside of that I don't have a lot of low level experience with it. I am also in the minority that believes Scala codebases are simpler than all the Java codebases I have come across. Maybe it's just me :D

I didn't know Zig can provide memory safety using it's features. If that's the case it's great, I need to try it out! Although I am sceptical about the claim that in truly safety-critical domains Rust would not be adopted where Zig would, but I am again out of my depth here, so :)

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @09:22AM (#25621909)
> I didn't know Zig can provide memory safety using it's features.

Yes, although this is done through runtime checks (that can be optionally disabled) rather than through static sound guarantees (Rust uses runtime checks in some cases, too). The language's simplicity and fast compilation helps with other types of correctness, too (i.e. beyond memory safety).

> Although I am sceptical about the claim that in truly safety-critical domains Rust would not be adopted where Zig would

I don't know if Zig would, but Rust has several problems for such domains that C and Zig don't: porting it is a challenge (both Rust itself and LLVM are very complex), understanding what a program does is a challenge (a lot of implicit behaviour), and handling allocation failures is a challenge.

Re:Zig in 30 Minutes by pjmlp on Sunday January 03, 2021 @11:14AM (#25622590)
Hopefully during 2021, LLVM won't be any longer the only option to compile Rust, as the work for integrating cranelift backend into stable keeps going on.

Zig also has the same issue with LLVM until the language is fully bootstraped and the original LLVM based compiler no longer part of the build process.

However, packaging a C and C++ compiler alongside for the ride doesn't make it that much simpler.

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @12:29PM (#25623220)
> LLVM won't be any longer the only option to compile Rust, as the work for integrating cranelift backend into stable keeps going on.

That might not matter. Porting Rust might not be a simple undertaking, either.

> Zig also has the same issue with LLVM until the language is fully bootstraped and the original LLVM based compiler no longer part of the build process.

Yes, but that's expected to happen before 1.0.

> However, packaging a C and C++ compiler alongside for the ride doesn't make it that much simpler.

C isn't an issue at all; C++ is. I'm pretty sure it could be made optional.

Re:Zig in 30 Minutes by DixieDev on Sunday January 03, 2021 @08:52AM (#25621757)
I like Rust for the reason you've stated, but it is absolutely both complex and full of potential for implicit behaviour. `Copy`, `Deref`, and `Drop` traits immediately spring to mind, but there are also weirdly specific things like having `no_mangle` and `static` on a global variable when targeting WASM implicitly exports (the address/offset of) the variable for use on the JS side.

Procedural macros are also somewhat insane in many ways.

Re:Zig in 30 Minutes by friend-monoid on Sunday January 03, 2021 @07:36AM (#25621418)
Try compiling a program with nostd and nocore - try targeting a weird platform, like a small embedded device. You immediately find out how many hidden layers there are to Rust, it’s borderline annoying. Rust is an amazing language, but stuff like mem transmute and I don’t know - it’s been a while now - just trips you up. It’s possible that I just don’t “get” rust, but maybe that’s also the problem. Zig is so easy it’s hard to get wrong. Zig favours readability and understandability over everything else. I can easily grasp complex questions like “will this overflow my very limited call stack” in Zig - it’s harder in Rust (for me anyway).
Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @09:34AM (#25621977)
> It seeks to be as simple as C

It seeks too, but fails. Comparing to really-simple C, it's full of arcane stuff.

> Zig rejects any kind of implicitness; all calls must appear as calls.

We already have that in C.

> Of course, that illusion breaks as soon as you try to change code

That's one of the traits of Zig too it seems. My favorite example of Zig's poor design is following case:

Suppose you have a function call:

    collectNoInline(gc);
And you want to annotate it for being never-inlined (call-site level annotation for "always inline" and "never inline" is a customary enough task in low-level programming). So, in Zig, annotated call transforms too:

    @call(.{ .modifier = .never_inline }, collectNoInline, .{gc});
Ref: https://ziglang.org/documentation/master/#call

I'm sorry, but that's total nonsense. Shows that not enough good faith went into Zig design. Even in C, that could be syntactically annotated as:

    /*pragma:noinline*/
    collectNoInline(gc);
Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @10:09AM (#25622157)
> Comparing to really-simple C, it's full of arcane stuff.

I think it's quite the opposite. Don't forget that C also has macros and a lot of arcane undefined behaviour. I don't know if Zig is as simple as C -- it doesn't have macros but does have, say, slices and comptime -- but it's also much safer and much more expressive. Overall, it's a very simple language, that's very expressive and also quite safe.

> That's one of the traits of Zig too it seems.

Absolutely! It's a feature of all low-level languages. Zig, however, unlike C++ and Rust, doesn't try to waste its complexity budget on making it seem like a high-level language when you just look at the code on the page (and it has other downsides, too, besides language complexity).

> I'm sorry, but that's total nonsense. Shows that not enough good faith went into Zig design. Even in C, that could be syntactically annotated as:

And I'm sorry, too, but I would say it's an example of Zig's exceptionally great design at this stage. If at some point it turns out that adding a new kind of syntax element here is worthwhile, it can always be added later. When in doubt -- leave it out. You never know when your complexity budget is best spent, and if you already have a general means of doing something without a new syntactic category, it's best to wait and see.

Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @11:06AM (#25622534)
> I think it's quite the opposite. Don't forget that C also has macros and a lot of arcane undefined behaviour.

C lexic-based "macros" are implemented by a separate component, "C preprocessor". C clearly needs a AST-based macros, not a whole new language to replace it. Undefined behavior comes from the fact that C is machine-independent assembler (with structured syntax), so to be exactly that ("machine-independent" and "assembler"), it has to underspecify machine-specific matters, like what happens if you shift 32-bit number by 33 bits.

> I don't know if Zig is as simple as C -- it doesn't have macros but does have, say, slices and comptime -- but it's also much safer and much more expressive. Overall, it's a very simple language, that's very expressive and also quite safe.

Those are points which I doubt - it's clearly not expressive enough, and there're safer languages either. Is there niche for it? I'm on a fence with popcorn. But as I look to it, I see things which I consider "mistakes". And I love to talk about mistakes!

> And I'm sorry, too, but I would say it's an example of Zig's exceptionally great design at this stage.

Expectedly, I don't buy this "worse is better" argument either. It's not good when you annotate a simple call and it turns from a simple call into an ugly mess. It's not rocket science to come with a syntax which would avoid that. If Zig language creators didn't do that so far, that's exactly what I call "not enough good faith went into Zig design".

Your appendix of "so far" is accepted - keep up the great work!

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @12:34PM (#25623267)
> C clearly needs a AST-based macros, not a whole new language to replace it.

But that "whole new language" is probably smaller than just AST macros for C. And C's problems aren't solved just by fixing macros (I think AST macros are a horrible feature in almost any language that isn't a Lisp; I see them as a sign that the language designers just gave up).

> Undefined behavior comes from the fact that C is machine-independent assembler

True, but that's almost never a valuable thing these days; you lose portability and get little in return other than a somewhat smaller compiler.

> it's clearly not expressive enough

It's at least as expressive as any other low-level language. Any C++/Rust program -- with generics, concepts, destructors, async, traits, macros -- could be translated to an equivalent Zig program that's at worst no longer than some (small) constant multiple of the original. That's not true for C (at least not without hurting performance).

> and there're safer languages either

Low-level? Not so sure. The goal is a language that makes it easy to write correct programs, not "a safe language". I'm not sure there are languages that do that better.

> I don't buy this "worse is better" argument either

It's an "it's just better" argument. Good design is not about picking arbitrary things and making them more convenient, but about spending a certain budget in a way that yields a good gestalt.

> It's not rocket science to come with a syntax which would avoid that.

Sure, but you can add that syntax at any time. It's not urgent. It's best to see if you could perhaps generalise it in some way and get a better return, or maybe invest your complexity budget more wisely elsewhere. I doubt compiler directives make up more than 0.1% of a program.

> If Zig language creators didn't do that so far, that's exactly what I call "not enough good faith went into Zig design".

I've been programming for ~30 years, and aside from Scheme -- maybe -- I don't think I've ever encountered a better-designed language than Zig. Of course, this perception is subjective.