News for hackers. Stuff that matters.
Re:Why Zig when there is already C++, D, and Rust? by jkelleyrtp on Friday January 15, 2021 @06:15PM (#25797888)
I love both Zig and Rust - I believe they both serve different types of systems programming - but for many developers, I'm not sure how important these points are.

Zig's reasons for being better include:

- No operator overloading

- Rust panics when allocator fails

- Rust uses a std allocator (which is configureable)

- No metaprogramming (IE, macros)

- Simpler to grok

Essentially, Zig being the "Go" of C/C++/Rust/D. But, I think that dances around the problem where Systems Programming is just complex. Just because some functions and control flow is simpler doesn't mean the program itself is.

If trying to pull Rust programmers away, I'm not sure the merits in this article are strong enough. However, for newcomers to systems programming, Zig might be easier to learn and solve problems with.

Re:Why Zig when there is already C++, D, and Rust? by slezyr on Friday January 15, 2021 @06:20PM (#25797950)
> - No operator overloading

Ehh??? It's the first point there.

And I don't like all this hate for operator overloading. I prefer to write "vec1 * (vec2 + vec3)" instead of "vec1.mul(vec2.plus(vec3))".

Re:Why Zig when there is already C++, D, and Rust? by hansvm on Friday January 15, 2021 @07:45PM (#25798704)
I know a lot of people don't like overloading in general because they've been bitten by people abusing it, but I don't think that's where Zig is coming from. Rather, the project places a high value on being able to understand a snippet of code in isolation, particularly the performance implications of that snippet.

Especially in math-heavy domains I don't think anyone is arguing that "vec1.mul(vec2.plus(vec3))" is cleaner code in the abstract (and I know I've personally written pre-processors _entirely_ to avoid having to write that kind of garbage when doing math-heavy code in an environment unfriendly to such syntax), but the function calls make it crystal clear that something non-trivial is happening under the hood.

Do I want to to give up operator overloading in Python? Absolutely not, and to be frank I wish that portion of the language were even more dynamic. Do I care about Zig not having operator overloading? Not in the slightest. It sits at a different point in the language design space, and I'm super excited about it.

Re:Why Zig when there is already C++, D, and Rust? by Dylan16807 on Friday January 15, 2021 @09:10PM (#25799269)
> the function calls make it crystal clear that something non-trivial is happening under the hood

Alright well I'd also rather write "vec1 [*] (vec2 [+] vec3)" too. Or equivalent.

Re:Why Zig when there is already C++, D, and Rust? by hansvm on Friday January 15, 2021 @10:20PM (#25799716)
Zig is still in development, and they're not necessarily opposed to an idea like that. I agree that could be a nice addition.

https://github.com/ziglang/zig/issues/427

Re:Why Zig when there is already C++, D, and Rust? by higerordermap on Friday January 15, 2021 @10:42PM (#25799827)
This is a nice idea
Re:Why Zig when there is already C++, D, and Rust? by creata on Friday January 15, 2021 @07:48PM (#25798721)
> the function calls make it crystal clear that something non-trivial is happening under the hood.

Well, it makes it crystal clear that something non-trivial may be happening under the hood. If I have a vector type which is implemented using SIMD intrinsics, I'd still call its addition operation "trivial", even if it hasn't been blessed by the language as such.

Re:Why Zig when there is already C++, D, and Rust? by hansvm on Friday January 15, 2021 @08:08PM (#25798853)
The point that we only know something non-trivial _may_ be happening is definitely fair :) I'll leave the original comment as-is since you've already responded.

While we're nit-picking, a wide vector type implemented with SIMD intrinsics would still have a non-trivial addition as far as Zig is concerned; your specific example really only holds for sufficiently primitive vectors.

Re:Why Zig when there is already C++, D, and Rust? by kortex on Saturday January 16, 2021 @10:30AM (#25802801)
My beef with operator overloading is googling symbols and when I can't inspect it with a language server / IDE. In Pycharm/Clion you can jump to the dunder definition of the operator. Haskell lets you get :info on symbols.

Google has gotten much better at handling symbols.

Basically, custom infix operators are super convenient as long as they are auditable and aren't abused.

Re:Why Zig when there is already C++, D, and Rust? by mhh__ on Friday January 15, 2021 @06:23PM (#25797985)
I for one have literally never been bitten by it anywhere. I get that you could launch missiles with it but ultimately that's true of literally any structured programming.
Re:Why Zig when there is already C++, D, and Rust? by electrograv on Friday January 15, 2021 @07:21PM (#25798511)
Allow me to introduce you to C++’s new std::filesystem::path class, which overloads the divide operators "/" and "/=" to concatenate two paths. (Not to be confused with the “+=“ operator, which also concatenates paths, but with subtly different behavior.)

Why did the division symbol get chosen for a concatenation behavior, of all things? Well, I suppose because it looks kind of like how we write paths with slashes, e.g. “dir/subdir/etc”. While I understand how this might be fun aesthetically once you already know the types involved, I find this completely opposite-than-numeric behavior here to be quite ambiguous and unintuitive.

Re:Why Zig when there is already C++, D, and Rust? by logimame on Saturday January 16, 2021 @12:33AM (#25800318)
Actually, slashes in filesystem::path is one of the operator overloading uses I really like in the C++ standard library (It’s time-saving, it’s useful, it’s easy to read). What I really don’t like is the IO operators << and >>, which I think is an incredibly horrid language design mistake. (It’s slow in performance, it’s hard to add formatting options, hard to read).
Re:Why Zig when there is already C++, D, and Rust? by edflsafoiewq on Friday January 15, 2021 @07:33PM (#25798595)
Python's pathlib does that too. It doesn't sound like it's bitten you though; it's just an aesthetic disagreement.
Re:Why Zig when there is already C++, D, and Rust? by electrograv on Friday January 15, 2021 @07:50PM (#25798735)
I suspect it might have bitten me if I wasn’t so lucky to encounter “/=“ first, which lead me to read the documentation and discover the different kinds of concatenation (“+=“ and “/=“) and their behavior.

To someone just skimming the code though, it might not at all be obvious that “+=“ and “/=“ both exist as concatenation operators, and their behaviors are different.

But yes it’s an aesthetic preference or opinion; I tend to prefer operators only when their only possible behavior is nearly so obvious that no documentation should be necessary. When that’s not the case, I prefer named functions/methods because they permit being explicit about subtle differences.

Re:Why Zig when there is already C++, D, and Rust? by edflsafoiewq on Friday January 15, 2021 @08:07PM (#25798843)
There are actually named member functions for those; they're append and concat. Can you guess which is which? I can't. append is actually "append with directory separator", which is weird, because I'd expect append to be a string-like append to match the append function on std::string. Instead concat is a string-like append despite the fact std::string doesn't have a concat member.

OTOH / is an instant mnemonic cue for "append with separator", and + matches the + on std::string.

Re:Why Zig when there is already C++, D, and Rust? by electrograv on Friday January 15, 2021 @09:12PM (#25799291)
Yes, I 100% agree with you that “append()” vs “concat()” are certainly no less confusing in their differences than “/=“ vs “+=“ (and the latter are arguably less confusing due to the mnemonic effect you mention).

In fact, to generalize, I think we can fairly say that operator overloads are akin to extremely short function names; in some cases they may work out great when there is not much ambiguity implicit in the underlying problem, but in other cases a longer and more explicitly descriptive name is required to disambiguate. In this case, “append” and “concat” seem quite poor names for different functions, given that they are virtually synonymous and therefore do nothing to describe or distinguish their differences of behavior.

So my claim is that we can do significantly better at resolving ambiguity with carefully named functions (or other approaches) than operators, or tersely/ambiguously named functions (like “append” and “concat”). Of course, this does come at the cost of code verbosity. Just where we should draw the line between too ambiguous vs too verbose, is of course a difficult subjective matter.

Re:Why Zig when there is already C++, D, and Rust? by secondcoming on Friday January 15, 2021 @07:34PM (#25798603)
To be honest, I don't like the << operator either. I doubt I'll like the (optional) use of | for ranges too.
Re:Why Zig when there is already C++, D, and Rust? by mhh__ on Friday January 15, 2021 @07:41PM (#25798662)
<< was necessary at the time
Re:Why Zig when there is already C++, D, and Rust? by edflsafoiewq on Friday January 15, 2021 @07:44PM (#25798695)
Why?
Re:Why Zig when there is already C++, D, and Rust? by mhh__ on Friday January 15, 2021 @08:22PM (#25798962)
C++ didn't have variadic templates so passing along a stream with an operator was more ergonomic enough and typesafe.
Re:Why Zig when there is already C++, D, and Rust? by edflsafoiewq on Friday January 15, 2021 @08:36PM (#25799044)
I suppose you're comparing against std::format, but the question is why overloading an operator (cout << "foo") would have been necessary vs a regular member function (cout.put("foo")).
Re:Why Zig when there is already C++, D, and Rust? by mhh__ on Friday January 15, 2021 @08:56PM (#25799168)
Because it had to replace people using a comma inside printf. The member function could only accept one argument cleanly so you would've to chain them like cout.put.put.put
Re:Why Zig when there is already C++, D, and Rust? by mcintyre1994 on Friday January 15, 2021 @06:32PM (#25798086)
I was once trying to diagnose a performance issue in an algorithm written in Ocaml. Someone had overloaded ** to be (IIRC) 64-bit multiply. I had a momentary “gotcha in 2 seconds!” moment before realising what was happening with that one.
Re:Why Zig when there is already C++, D, and Rust? by andrepd on Friday January 15, 2021 @06:58PM (#25798301)
Funny you should mention OCaml, with doesn't have operator overloading! You can only have one function with a given name in scope at any point, that includes operators. Of course you can redefine with `let (+) a b = ...`, but then you have to explicitly open that module where you want to use that redefined operator. That makes it even more clear what's going on.
Re:Why Zig when there is already C++, D, and Rust? by whatshisface on Friday January 15, 2021 @06:21PM (#25797962)
Operator overloading is hated among programmers who never use math; in that community all uses of "+" are to do something other than addition, usually something that's not even remotely related to addition. Operators end up as one-character infix method names.
Re:Why Zig when there is already C++, D, and Rust? by colejohnson66 on Friday January 15, 2021 @06:25PM (#25798011)
I don’t think I’ve ever seen a library overload math operators when they didn’t do what you’d expect. And just because a language feature is abused doesn’t mean it’s bad. Typescript’s generics are Turing Complete, but they’re still great.
Re:Why Zig when there is already C++, D, and Rust? by whatshisface on Friday January 15, 2021 @06:28PM (#25798040)
Look at it this way - let's say you're a programmer who never uses math. As a result, 100% of your exposure to operator overloading will be, by definition, to cases that have nothing to do with math. While a programmer that uses math will see 1,000,000 good uses and 10 bad uses, a programmer who never uses math will see 0 good uses and 10 bad uses. I saw a similar effect in the Go community where they were debating whether or not the complex type should be removed. It's absolutely crucial for signal processing, but to the rank and file REST-ist, it's just another annoying case to type out in a generic-less language.
Re:Why Zig when there is already C++, D, and Rust? by Quekid5 on Friday January 15, 2021 @06:30PM (#25798063)
What programmer never uses math?

I mean, most of us aren't going use linear algebra, but still...

Re:Why Zig when there is already C++, D, and Rust? by whatshisface on Friday January 15, 2021 @06:36PM (#25798120)
I'm using math in a colloquial sense to refer to "mathy math," not "computer science math." So linear algebra would be a leading example, along with stuff like vectors and complex numbers. Accounting math would be another example, because it requires a bignum implementation that doesn't drop decimals like float can.
Re:Why Zig when there is already C++, D, and Rust? by Quekid5 on Friday January 15, 2021 @06:37PM (#25798132)
Ah! Might be helpful to specify that up front. Even so: Interval math could be quite helpful for lots of things that ... aren't using it.
Re:Why Zig when there is already C++, D, and Rust? by hnlmorg on Friday January 15, 2021 @07:00PM (#25798317)
> Might be helpful to specify that up front

I thought his post was pretty clear tbh. For example he mentions complex types so that should be an indicator that he's not talking about the kind of high school maths that the average developer can coast along with.

Re:Why Zig when there is already C++, D, and Rust? by im3w1l on Saturday January 16, 2021 @02:42AM (#25800800)
Do you have an example of someone solving a real-world problem with interval math? I've only ever seen toy examples and theoretical justifications.
Re:Why Zig when there is already C++, D, and Rust? by Quekid5 on Saturday January 16, 2021 @04:08PM (#25805524)
1) Most of (micro-)benchmarking in computing. Of course you want a tiny bit more analysis in that case.

2) Slightly more real-world: Measuring things when you only have approximate rulers, have difficult things to measure (odd surfaces, etc.), or have to calculate from 2nd hand measurements (pictures with rulers, etc.)

Not sure if that qualifies as Real World, but...?

Re:Why Zig when there is already C++, D, and Rust? by im3w1l on Saturday January 16, 2021 @11:28PM (#25808122)
By real world I mean code running in production and making someone money. Or a popular open source package. And I'd be interested in which company or project that is and if they have a writeup.
Re:Why Zig when there is already C++, D, and Rust? by hobofan on Friday January 15, 2021 @06:44PM (#25798190)
So for those use cases where you will be using a library with custom types anyway, choose one that doesn't overload the operators for those types? This very much seems like a problem on the library- and not the language-level.
Re:Why Zig when there is already C++, D, and Rust? by Ar-Curunir on Friday January 15, 2021 @07:39PM (#25798645)
Also, in Rust if you don't want to use the overloading, you can still write your code to directly use function calls `a.add(b)`, where `a` is a type that implements the `Add` trait.
Re:Why Zig when there is already C++, D, and Rust? by Arelius on Friday January 15, 2021 @06:52PM (#25798257)
I get the truth in that statement. But as a game programmer, I find it interesting the contrast that linear algebra is precisely what is commonly used.
Re:Why Zig when there is already C++, D, and Rust? by Quekid5 on Saturday January 16, 2021 @04:12PM (#25805554)
Hah! Touche!

Weird to think that game programming is (weirdly) kind of a niche thing at this point. Compared to programming boring CRUD apps, that is. :)

Re:Why Zig when there is already C++, D, and Rust? by cycomanic on Saturday January 16, 2021 @09:08AM (#25802221)
I encountered something similar when I was trying out Nim. In Nim - ^ is the exponentiationoperator, so x^2 means x squared. The way operator precedence is implemented in Nim is that the unary negate (-) takes precedence over the ^, so - 2^2=4. It threw me off quite a bit (coming from a physical science background I assumed it should always be - 4,took a while in my debugging to figure this out). I went to their discord (?) channel and asked about this, some of the people there could not understand how it would be any other way, which just shows that the way someone who largely translates math into code (like myself) things about code very differently then someone who never does this.
Re:Why Zig when there is already C++, D, and Rust? by amluto on Friday January 15, 2021 @06:29PM (#25798046)
Have you ever used Boost?
Re:Why Zig when there is already C++, D, and Rust? by lttlrck on Friday January 15, 2021 @06:44PM (#25798193)
First thing that came to mind was Boost Spirit: https://www.boost.org/doc/libs/1_48_0/libs/spirit/example/qi...

Lovely.

Re:Why Zig when there is already C++, D, and Rust? by codeflo on Friday January 15, 2021 @07:28PM (#25798560)
Ah, the memories: I once cut the compile time of a multi-million line C++ codebase by over 20%, just by removing one single-line use of Boost Spirit and replacing it with atoi.
Re:Why Zig when there is already C++, D, and Rust? by munk-a on Friday January 15, 2021 @06:30PM (#25798065)
I'd rather solve this with code standards around function naming conventions (including symbols as names for functions) than remove a language feature.

I don't override operators often, but when I do it's useful because the operation I'm describing is a really close parallel to other uses and properties of that operation. As an example, it's why I dislike + for list append, + is normally commutative (which list appending definitely isn't) and subtraction is pretty bonkers as a reflected operator on lists.

Re:Why Zig when there is already C++, D, and Rust? by a1369209993 on Friday January 15, 2021 @09:40PM (#25799466)
FWIW, I advocate ~ as array append (a la perl6/raku strings) (and also as wedge product on vectors, * being dot product). It works well with x^y as exponent/repeat, since the rhs is qualitatively different anyway[0], and also gives you (with +) a sum-of-products/dioid/semiring structure for concatenation and alternation on regular expressions. Admittedly, you still don't have a sensible semantics for division, but that's true of plenty of other product operators (even int is iffy - either not closed (div-as-float/rational/etc) or not a multiplicative inverse (divmod)).

0: In particular, exponentiation by natural numbers (or positive integers for things like nonempty lists that deliberately exclude the multiplicative identity) is almost always well defined[1], even if the thing you're exponentiating bears no resemblance to a natural number.

1: Although note than with vectors and dimensional quantities (eg meters), x^2/x^3/etc are all different types: area/volume, bivector, etc.

Re:Why Zig when there is already C++, D, and Rust? by codeflo on Friday January 15, 2021 @07:23PM (#25798520)
I get your point. But even in non-mathy business software, data structure access is so much more convenient in languages with an overloadable index operator:

C#: foo[index] = bar[index];

Java: foo.put(index, bar.get(index));

Re:Why Zig when there is already C++, D, and Rust? by whatshisface on Friday January 15, 2021 @07:32PM (#25798591)
That is a very good point that I hadn't considered, because I was thinking about Go. In Go, the builtin data structures are compiler special-cases, so you get the index behavior on the map structure. Of course, it starts looking like Java for anything more advanced, but the ethos of Go is to not use anything more advanced.
Re:Why Zig when there is already C++, D, and Rust? by hctaw on Friday January 15, 2021 @07:40PM (#25798654)
There are more operators than the infix mathematical ones, like increment/decrement (cursors, iterators, custom counters), dereferencing (used for some clever pointer types), etc.

Sometimes there are good reasons to mask complexity.

Re:Why Zig when there is already C++, D, and Rust? by Impossible on Friday January 15, 2021 @06:57PM (#25798295)
I think this is the main reason. Outside of math libraries (and maybe strings for concatenation?) operator overloading rarely makes sense. If you're working in a math heavy domain with custom types it's a godsend though. I wouldn't want to work in a language without operator overloading (I've done enough work with Processing and a Quake derived game engine to know that it sucks), but others might have seen it horribly abused
Re:Why Zig when there is already C++, D, and Rust? by gnramires on Friday January 15, 2021 @08:30PM (#25799016)
And I would go even further!

I have been thinking recently that everything should (or rather could, in a special language) be overloadable. And every operator should be treated as a non-first class citizen of a language, '=', '+', and all. The inelegance is granting those operators any privileged status. So the parsing can be in every case dependent on the arguments being parsed: if you're adding numbers, number addition, if you're adding vectors; even more complex behavior could be added for special parsing cases (which I don't even know what could be), say for creating certain algebraic operators with special conditions, maybe something like knuth's up-arrow.

Of course, thought must be employed on the scoping of these parsing changes but since this behavior would be conditional on the argument properties, it is very difficult to see any problems. For example, while adding vectors overloads '+', it is not going to cause problems in other cases (when the arguments aren't vectors), and when dealing with vectors 'vec1+vec2' the programmer will essentially always be thinking about the overloaded operation anyway, it seems absurd a confusion would occur. I should be able to write 'x=5!+3' to mean 'x=factorial(5)+3'.

Allowing contextual meaning (evaluation) of operators, functions, even syntax, allows more compact, expressive language, because we can reuse words, associate their slightly different applications and adapt syntactical behavior to the problem at hand.

Take the usages of the word 'slow' in natural language: it can be a description of current velocity (variable) of an object (context-dependent) "the car is slow", it can be a description of a property of an object (low typical/maximum velocity) "slugs are slow", it can be a verb "please slow down", and so on. Creating new words for each use case is inefficient and disregards the natural close association of their (contextual and algorithmic) meaning.

Re:Why Zig when there is already C++, D, and Rust? by pjmlp on Monday January 18, 2021 @05:53AM (#25820430)
This is what happens in Lisp and Smalltalk for example.
Re:Why Zig when there is already C++, D, and Rust? by pharmakom on Saturday January 16, 2021 @04:46AM (#25801226)
I don't understand this benefit. so in zig we are trusted to manage memory but we can't be trusted to overload operators in a sensible way?
Re:Why Zig when there is already C++, D, and Rust? by generichuman on Saturday January 16, 2021 @06:08AM (#25801524)
The benefit is that you don't have to be concerned about hidden function calls. You don't get unexpected function calls or performance hits this way.

If you want your code to run fast, you need to ensure that you know what your program is doing and you're in control, at all times. Manual memory management enables that.

Re:Why Zig when there is already C++, D, and Rust? by pharmakom on Saturday January 16, 2021 @09:58AM (#25802573)
... but operator overload is not really different to function overloading, it's just that the function name is infix rather than prefix.
Re:Why Zig when there is already C++, D, and Rust? by jorangreef on Saturday January 16, 2021 @06:05AM (#25801516)
I believe the rationale against operator overloading is not a question of whether the programmer should or shouldn't be trusted, but rather simply about optimizing for code readability.
Re:Why Zig when there is already C++, D, and Rust? by pjmlp on Monday January 18, 2021 @05:55AM (#25820439)
Then better integrate some static analysis with natural grammar knowledge to ensure all functions are properly named for their actual purpose.
Re:Why Zig when there is already C++, D, and Rust? by haberman on Friday January 15, 2021 @06:52PM (#25798254)
> Essentially, Zig being the "Go" of C/C++/Rust/D.

I'm not sure that's a fair comparison, as Zig features compile-time evaluation ("comptime") that supports first-class types. Generics falls out of that by letting you create types imperatively at compile time. It's hard to imagine Go ever going in this direction.

As a long-time C programmer, I am really excited to try out Zig once it stabilizes a bit more. Zig's take on generics seems like a compelling alternative to C++ templates.

Re:Why Zig when there is already C++, D, and Rust? by potta_coffee on Friday January 15, 2021 @06:21PM (#25797956)
I'm much more interested in Zig than Rust, but I'm also the kind of guy that is attracted to C over C++, and I also really like Go. I actually don't care about arguing the merits of one over the other, I'm just really happy and excited that there are cool new languages out there and it seems like here's really something for everyone.
Re:Why Zig when there is already C++, D, and Rust? by 7thaccount on Friday January 15, 2021 @06:44PM (#25798198)
Newcomers to systems coding need a really good intro guide as there are so many new things to learn (Ex: pointers, freeing memory). With high level languages, the syntax and garbage collector do everything for me. I build complex and nested data structures in Python daily and have no idea how to do the equivalent in C. Zig seems to assume you already know C, so I never see it winning the onboarding crowd. This is also why Clojure, Scala, F#, Rust...etc will struggle to get big.
Re:Why Zig when there is already C++, D, and Rust? by kristoff_it on Friday January 15, 2021 @06:53PM (#25798266)
There's a lot to do, sure, but while we don't have yet swats of learning materials for complete newcomers, I consider Zig to be today the cheapest way of getting into "systems" programming for people that never did it before.

I run Zig SHOWTIME [1] and we have quite a few talks that introduce basic concepts to people that aren't used to systems programming. Again, not yet enough to consider the problem solved, but it's a step in the right direction and we'll build a bigger library as more people join the community.

[1] https://zig.show

Re:Why Zig when there is already C++, D, and Rust? by yellowapple on Friday January 15, 2021 @09:08PM (#25799253)
I'd say my (basic) understanding of Zig has actually helped me better understand C, since its semantics around pointer operations and allocation are a lot more explicit and intuitive; it's really helped make things "click" for me. Plus, the build system is infinitely better than I've seen from any native-compiled language, period.

Those two factors make me think it's close to the ideal language for newcomers to lower-level programming. Even the syntax ain't bad, as far as C-like syntax goes; it'd be interesting to see some alternative languages that put alternate syntaxes over Zig's semantics, though (and one of my New Year's Resolutions is to put together something to that effect, whether by transpiling to Zig or by (ab)using Zig's compile-time programming).

Re:Why Zig when there is already C++, D, and Rust? by jorangreef on Saturday January 16, 2021 @03:32AM (#25800966)
Actually, something that I appreciate about Zig is how approachable it is for people coming from TypeScript/JavaScript.

Zig is insanely easy to read coming from high level languages like these. It's only when you start to write Zig that you need to take more time to understand, for example, the difference between the lifetime of a pointer to stack allocated memory vs a pointer to heap allocated memory.

But by then, I think Zig will have already hooked you.

Zig is also a great way to learn systems programming, because the design of the language and the std lib code itself is such a great teacher.

Re:Why Zig when there is already C++, D, and Rust? by timClicks on Friday January 15, 2021 @06:56PM (#25798289)
This problem was exactly why I wrote Rust in Action. It teaches you systems programming alongside teaching Rust.

That said, you can get quite far without needing to understand pointers or memory management in any of the example languages you mentioned - "Clojure, Scala, F#, Rust". In the first 3, the garbage collector still does most of the work. In the last, there is are no calls to malloc() or free(). Rust's compile-time lifetime concept allows to compiler to do the work for you.

Re:Why Zig when there is already C++, D, and Rust? by 7thaccount on Friday January 15, 2021 @09:43PM (#25799499)
I should've been more specific. It's a different problem with Clojure, Scala, & F# where you're expected to understand the JVM & Java or the CLR & C#. It seems similar to Zig to me. There are numerous sources on learning C and I can barely figure out how to do a lot of things in Zig. Maybe a learn X in Y would help me.
Re:Why Zig when there is already C++, D, and Rust? by pjmlp on Saturday January 16, 2021 @04:23AM (#25801150)
If you already know any JVM or .NET language, the first step would be to understand the full stack, you don't need C for that.

Many of us were doing systems programming with other languages before C went mainstream.

What you need to learn is computer architecture.

Getting back to JVM or .NET, you can get hold of JIT Watch, VS debug mode or play online in SharpLab.

Get to understand how some code gets translated into MSIL/JVM, and how those bytecodes end up being converted into machine code.

https://github.com/AdoptOpenJDK/jitwatch/wiki/Screenshots

https://sharplab.io/

Languages like F# and C# allow you to leave the high level comfort and also do most of the stuff you would be doing in C.

Or just pick D, which provides the same comfort and goes even further in low level capabilities.

Use them to write a toy compiler, userspace driver, talking to GPIO pins in a PI, manipulating B-Tree data stuctures directly from inodes, a TCP/IP userspace driver.

Not advocating not to learn Zig, do it still, the more languages one learns the better.

Only advocating what might be an easier transition path into learning about systems programming concepts.

Re:Why Zig when there is already C++, D, and Rust? by 7thaccount on Saturday January 16, 2021 @07:55PM (#25807062)
No.

With Python, Perl,R, Bash... whatever, I never had to know any C to be very productive.

With F#, good luck not knowing C#. With Clojure, not knowing Java is a pain. I've tried both several times and was frustrated by constantly being referred to something Java related that I didn't understand. Because of that, I feel like those languages will never pick up anyone outside of regular users of C# or Java that want to try something different. Of course there are exceptions, but they're just that.

I agree with you that perhaps one should go through all of what you're referring to Ex: learn assembly, write a Forth...etc.

I'm just saying a lot of these supposedly amazing languages stagnant with regards to their userbase for a reason. APL uses an entirely different way of doing things than Clojure, but I'd bet I could get up to speed a lot faster with APL than Clojure where I constantly have to fight with learning the daunting ecosystem.

Re:Why Zig when there is already C++, D, and Rust? by mhenders on Friday January 15, 2021 @10:42PM (#25799823)
I enjoyed reading through https://ziglearn.org/
Re:Why Zig when there is already C++, D, and Rust? by timClicks on Sunday January 17, 2021 @01:16AM (#25808610)
Wow, that looks like a great (& concise!) resource
Re:Why Zig when there is already C++, D, and Rust? by kindofajsdev on Friday January 15, 2021 @08:05PM (#25798829)
Why is no metaprogramming a good thing? I'm mostly a web dev but I've played with crystal quite a bit. Crystal embraces macros & metaprogramming. Its almost a language within the language.

Furthermore, isn't go a statically compiled language? Why isn't go comparable to c + others?

Re:Why Zig when there is already C++, D, and Rust? by jonahx on Saturday January 16, 2021 @12:58AM (#25800423)
As a ruby developer who went through a stage of being enamored with meta-programming, I'd argue that it's siren song: enticing, but dangerous and destructive. It's especially dangerous because it's fun to write -- it scratches the "I'm so clever" itch -- but is a disaster to read, and a disaster to maintain.

Aside from just wanting to be creative, the desire to do it usually indicates either a missing core language feature, or a programmer's missing knowledge of how to use core features effectively and idiomatically. Otherwise you'd use the simple core language feature (the one every developer is guaranteed to know how to use already) to achieve your goal.

The other reason it's sometimes used is prioritizing terseness over simplicity. That is, you introduce some meta-programming abstraction that saves you some LOC, but at the cost of abstraction overhead. Almost always, you're better off with the reverse priorities.

Re:Why Zig when there is already C++, D, and Rust? by iFreilicht on Saturday January 16, 2021 @06:15PM (#25806452)
Because what zig does is much more intuitive: it has a comptime keyword that allows you to execute almost any function at compile time. So you use the same language for programming and metaprogramming, there’s no weird second syntax to learn. Even better, they also use that approach for generics, where types are just comptime parameters. Again, makes the syntax more consistent and requires less different ways of thinking.
Re:Why Zig when there is already C++, D, and Rust? by kristoff_it on Friday January 15, 2021 @07:00PM (#25798316)
If anything, I'd argue that Rust is the "Go" of systems programming.
Re:Why Zig when there is already C++, D, and Rust? by slimsag on Friday January 15, 2021 @07:16PM (#25798473)
Really surprised to hear you say this, actually - in my mind Rust is the can of worms that allows almost anything and everything in terms of language features and will likely end up as bloated (if not more) than C++ in the next 20 years.

What makes you argue that Rust is the "Go" of systems programming?

Re:Why Zig when there is already C++, D, and Rust? by codeflo on Friday January 15, 2021 @07:37PM (#25798631)
For me personally, the problem with C++ isn't that it has too many features, but that they interact poorly and require complicated rules to makes them work together. To list a few pairs: destructors and exceptions, templates and inheritance, templates and headers, encapsulation and headers, namespaces and operator overloading.

At least at the moment, Rust's many features are mostly orthogonal and work well in combination. But in some sense, that makes the learning curve even harder, because you can't focus on an "accepted subset" as you would in an existing C++ codebase.

Re:Why Zig when there is already C++, D, and Rust? by kristoff_it on Friday January 15, 2021 @07:36PM (#25798617)
I wouldn't use this analogy as an everyday tool, but in reply to the comment above, if we really have to give the "Go" role to either Zig or Rust, I'd choose Rust for the main reason that, while you can certainly come up with a lot of abstruse abstractions, the language ultimately wants to keep you away from certain tools that would otherwise be considered core to a systems programming language. I'm of course referring to the restrictions that ensure safety at the expense of being able to implement some kinds of algorithms (in a "safe" way).

Go in may ways has the same approach: stop developers from doing potentially damaging things (according to Go's definition of what's "potentially damaging"), which I guess is one of the reasons why the creators decided to remove pointer arithmetic and add a garbage collector.

Zig wants to keep the language small, but the programmer is expected to have access to all kinds of advanced tools, in a way that I don't see neither Go nor Rust consider acceptable.

In Zig you can use `undefined` to avoid initializing memory, do all kinds of pointer arithmetic, and while you can't overload operators, you can use comptime to concoct all kinds of static/dynamic dispatch schemes, the only limit is just that they can't masquerade as basic operations.

Of course you can do all these things in Rust unsafe, and same in Go with cgo, but the point is not what you can or can not do, it's what the language guides you towards, and the only potentially damaging thing that Zig tries to prevent you from doing, is making code hard to understand for readers.

Re:Why Zig when there is already C++, D, and Rust? by pjmlp on Saturday January 16, 2021 @04:29AM (#25801173)
50 years of C and a few less of Objective-C and C++, have proven that C.A.Hoare was right regarding language defaults in computer security, in the approach taken by ALGOL based systems programming languages, starting with ESPOL/NEWP.

If I want unsafe languages, C and C++ already fulfil the purpose with 50 years of ecosystem.

Being safer than UNIX for certain kinds of customers, is one of the selling points of Unysis ClearPath MCP, thanks to NEWP.

Re:Why Zig when there is already C++, D, and Rust? by pharmakom on Saturday January 16, 2021 @04:48AM (#25801234)
Rust is the haskell of systems programming
Re:Why Zig when there is already C++, D, and Rust? by iFreilicht on Saturday January 16, 2021 @06:16PM (#25806460)
Great in theory but so convoluted and theoretical that it will always be niche?
Re:Why Zig when there is already C++, D, and Rust? by pharmakom on Sunday January 17, 2021 @07:29AM (#25810182)
I mean mostly in the sense that it takes mathematically modelling programming and the machine being programmed very seriously and it allows you to build tall towers of abstraction on top. Compare this to C++ for example where many of the mechanisms for building abstractions are accidental or C where abstractions are not really possible.
Re:Why Zig when there is already C++, D, and Rust? by iFreilicht on Sunday January 17, 2021 @11:27AM (#25812040)
Ah right, that makes sense, I read your comment in more of a snarky manner before.
Re:Why Zig when there is already C++, D, and Rust? by pjmlp on Sunday January 17, 2021 @07:14AM (#25810114)
Actually I think that in the long term most languages with automatic memory management will just adopt some form of affine types for high performance code sections, and that will be it.

We will keep the productivity of the existing eco-systems and tooling, with an additional tool for the 1% of use cases that it actually matters.

Re:Why Zig when there is already C++, D, and Rust? by iFreilicht on Sunday January 17, 2021 @11:26AM (#25812028)
If even that. I think in a lot of cases, it doesn't really matter, and most already allow linking to a library, which works well enough for Python at least, and should be fine for most application development purposes.

Languages like Rust and Zig are just on a different level, for cases where performance or hardware restrictions are the number one concern.

Re:Why Zig when there is already C++, D, and Rust? by lightgreen on Saturday January 16, 2021 @06:17AM (#25801553)
The largest difference between Zig and Rust is Zig does not have memory safety. This arguments is more important than all other differences combined. Because when writing very large long living projects, memory safety is the most important issue.
Re:Why Zig when there is already C++, D, and Rust? by jorangreef on Saturday January 16, 2021 @06:52AM (#25801678)
Zig does however go a long way towards memory safety, to be fair, and if you wanted to, you could also argue similarly that Rust does not guarantee 100% OOM safety whereas Zig does: https://www.youtube.com/watch?v=Z4oYSByyRak

I would also say that for "very large long lived projects", memory safety is not actually the most important issue, but rather correctness, followed by (in no particular order) safety, performance, explicitness, readability and exhaustive fine-grained error handling, the latter also not found in too many languages besides Zig.

Re:Why Zig when there is already C++, D, and Rust? by alserio on Saturday January 16, 2021 @07:37AM (#25801851)
Isn't memory safety a subset (and I would argue a necessary requisite) of correctness?
Re:Why Zig when there is already C++, D, and Rust? by jorangreef on Saturday January 16, 2021 @07:47AM (#25801888)
Good question.

Assuming we both mean "memory safety" as in a guarantee given by the language (e.g. Rust), then no, logically speaking, it can't be a requisite for correctness, and it's not even a subset of correctness.

Here's why:

If you can write a correct program in a language which does not guarantee memory safety (which we certainly could, for example, simply by not allocating memory at all, or not using pointers etc, or by using runtime checks e.g. to ensure there are no double frees or out of bounds reads/writes), then memory safety is neither a subset of, nor a requisite for correctness.

Memory safety is a double-edged sword. It can make correctness easier to achieve. But that also depends on how the language implements memory safety. If this is done by at the expense of a steeper learning curve, then that could in itself be an argument that the language is less likely to lead towards correctness, as opposed to say an almost memory safe language that implements 80% of this guarantee while optimizing for readability, and with a weekend learning curve.

Historically, the lack of memory safety has obviously been the cause of too many CVEs. But even CVEs in themselves are more a measure of security than correctness. I would say that exhaustive fine-grained error handling checked by the compiler is probably right up there for writing correct programs.

Re:Why Zig when there is already C++, D, and Rust? by alserio on Saturday January 16, 2021 @08:37AM (#25802085)
Thank you for your answer. You can certainly write a memory safe program in a language that doesn't guarantee memory safety. However I still maintain that correctness implies memory safety, at least as a characteristic of the program if not of the language. If your language doesn't help there you should expend more time and effort to achieve that result, and accept higher risk of screwing it up. But I see why you argue that readability correlates with correctness. It's also true that most memory safety issue are really hard to spot, and that may be even more true for 80%-safe language. I really like when the computer does work for me, since as a human I'm way more sloppy.
Re:Why Zig when there is already C++, D, and Rust? by jorangreef on Saturday January 16, 2021 @10:28AM (#25802786)
Yes, I fully agree that a correct program can't contain memory bugs, and that we want the compiler to help us.

For a systems programming language though, I think Zig hits the sweet spot, and not only with regards to memory safety.

Correctness, in this realm, is as much memory safety as:

* error safety (making sure that your program correctly handles all system call errors that could possibly occur, without forgetting any, and there are many! The Zig compiler can actually inspect and check this for you, something not many languages do), see https://www.eecg.utoronto.ca/~yuan/papers/failure_analysis_o... for how critical error handling is in distributed systems,

* OOM safety (making sure your program can actually handle resource allocation failures without crashing),

* explicitness (clear control flow with a minimum of abstractions to make it easy to reason about the code with no hidden surprises, no weird undefined behavior),

* and especially as much runtime safety as you can possibly get from the language when you need to write unsafe code (which you will still need to do when writing systems code, even if your language offers memory safety guarantees, see https://andrewkelley.me/post/unsafe-zig-safer-than-unsafe-ru...). Here, Zig helps you not only at compile time, but also at runtime (and with varying degrees of granularity as you see fit), something not all systems languages will do.

On all these axes, Zig is at least an order of magnitude more likely to lead to a correct program than C, while optimizing for more readable code (even for someone coming from TypeScript) and thus code review, also essential for improving the odds that your code is correct with respect to its requirements.

Re:Why Zig when there is already C++, D, and Rust? by alserio on Saturday January 16, 2021 @11:33AM (#25803283)
That's really quite cool! Thank you again for your analysis and links
Re:Why Zig when there is already C++, D, and Rust? by teleforce on Friday January 15, 2021 @06:41PM (#25798168)
Not really sure why the original HN poster left "D" from its title, is there any conspiracy going on to silence D :-)

Actually unlike C++ and Rust you can have a lean D and most probably it can perform as good as Zig if not better. D has excellent CTFE support similar to Zig and D has no macro as well. If you want extra memory safety with ownership support D has that feature covered.

Re:Why Zig when there is already C++, D, and Rust? by MaxBarraclough on Friday January 15, 2021 @06:50PM (#25798235)
> unlike C++ and Rust you can have a lean D and most probably it can perform as good as Zig if not better

I doubt this is really meaningful. The performance-ceiling on all four languages is presumably very high, as they all allow you to write hand-optimised low-level code if you want to.

Re:Why Zig when there is already C++, D, and Rust? by teleforce on Friday January 15, 2021 @08:03PM (#25798817)
What it is probably not obvious to most people is that D has supports for D as better C, so it can have seamless interaction with C library and vice versa [1]. With betterC, D supports for C interfacing and integration is second to none. Heck, you can even compile the D compiler as a library if you fancy a built-in compiler for your C and D program. This seamless integration capability can be very productive where most of the high performance libraries are written in C.

[1]https://dlang.org/blog/2017/08/23/d-as-a-better-c/

[2]https://dlang.org/blog/2017/08/01/a-dub-case-study-compiling...

Re:Why Zig when there is already C++, D, and Rust? by dralley on Friday January 15, 2021 @08:56PM (#25799160)
Zig has similar good integration with C. The Zig compiler is also a C compiler (calls into Clang), C headers can be imported directly from Zig code and used without writing any bindings, and you can also autogenerate C headers for Zig code so that it can be used directly from C (although this is currently broken).
Re:Why Zig when there is already C++, D, and Rust? by p0nce on Friday January 15, 2021 @07:26PM (#25798547)
Pretty much, so you could as well use the one you find most productive. If productivity is not there, fast programs don't materialize.
Re:Why Zig when there is already C++, D, and Rust? by MaxBarraclough on Friday January 15, 2021 @07:41PM (#25798664)
You could go for productivity, but that's not the only dimension. You could emphasise safety: C++ has no equivalent to Rust's safe subset. I believe D has a safe subset too, but I don't think Zig does. You could emphasise portability: C++ seems like the clear winner in terms of having the most supported platforms.
Re:Why Zig when there is already C++, D, and Rust? by pjmlp on Saturday January 16, 2021 @04:38AM (#25801200)
Microsoft and Google are trying to retrofit a safe subset into C++, via the Core Guidelines and lifetimes, enforced via static analysers.

Since C++ is more relevant to what I do, naturally I keep validating how good they fair and so far not good.

Lifetimes checker for example, is only able to deal with basic workflows and when it complains the error messages aren't that good, leaving you wonder why the analyser considered it an incorrect lifetime.

Main reason being, if the call sites aren't annotated, it can only guess the lifetime use, and mostly it gets it wrong.

No doubt if they keep throwing money at it, they might eventually solve it, however this is already a three year effort and this is the best so far.

Re:Why Zig when there is already C++, D, and Rust? by MaxBarraclough on Saturday January 16, 2021 @06:29AM (#25801603)
It's also possible to formally verify C++, [0][1] but this isn't commonly done. I imagine it's very labour intensive.

[0] https://www.eschertech.com/products/ecv.php

[1] https://trust-in-soft.com/