News for hackers. Stuff that matters.
Re:Zig in 30 Minutes by dbandstra on Sunday January 03, 2021 @01:33AM (#25619999)
I wouldn't call that careless. So far, proposals to extend the for-loop have not passed the cost/benefit test. I think supporting iterators would require something like magic-methods, which so far are not a thing in Zig (language syntax doesn't interact with userland code).

The status quo for iterators is this:

  var it = getSomeIterable();
  while (it.next()) |item| {
      ...
  }
IMO it's "good but not great" (not great because it adds a var and the variable pollutes the outer scope). But the alternative is a bit of a pandora's box.
Re:Zig in 30 Minutes by Leherenn on Sunday January 03, 2021 @06:30AM (#25621183)
Honestly, I know it's minor, but when I tried Zig the fact I couldn't do the classic for loop to do a "for (int i = 0; i < n; ++i)" was quite annoying. It pollutes the scope, so you need to add scope braces every time, and I find it's much easier to forget the ++i at the end of the while loop.

I understand where it's coming from, but having something like "for (i32 i in [0,n[)" would be so much nicer in my opinion.

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @09:01AM (#25621795)
> and I find it's much easier to forget the ++i at the end of the while loop.

You can do:

    var i: usize = 0;
    while (i < 10) : (i += 1) {
        ...
    }
or

    var i: usize = 0;
    while (i < 10) {
        defer i += 1;
        ...
    }
although the two have slightly different semantics (the latter would increment even after a break; the former wouldn't).
Re:Zig in 30 Minutes by jeltz on Monday January 04, 2021 @01:33AM (#25628415)
Given how Zig does not allow variable shadowing (which is a choice which makes sense on its own) I find the scope leak to be very annoying in practice.
Re:Zig in 30 Minutes by dilap on Sunday January 03, 2021 @09:47AM (#25622044)
Neither of those fix the scope leak, tho.
Re:Zig in 30 Minutes by AnIdiotOnTheNet on Sunday January 03, 2021 @10:35AM (#25622314)
This is true. There have been proposals to alter the `while` syntax to fix the scope leak, as well as proposals to extend `for` to support ranges, but none have been accepted (and they aren't very compelling in my opinion).

Honestly, I'm not sure why people think it is such a big deal. I've written a lot of zig, mostly gamedev stuff, and I rarely use that construction. I find that the vast majority of loops are over arrays or slices.

Re:Zig in 30 Minutes by dilap on Sunday January 03, 2021 @05:38PM (#25625902)
While I don't have much experience with Zig, I agree -- from my little bit of playing around, it also hasn't been a big deal.

(The biggest thing I've hit that I want is a way to return information with an error. If that were fixed, I think Zig's error handling would be perfect.)

Re:Zig in 30 Minutes by jorangreef on Monday January 04, 2021 @06:02AM (#25629790)
Loris Cro did a great talk on error handling in Zig, which also deals with how to return information with an error if you need to, and why the common case optimizes for simpler error values: https://www.youtube.com/watch?v=TOIYyTacInM
Re:Zig in 30 Minutes by dilap on Monday January 04, 2021 @09:18AM (#25631598)
Thanks, that was a nice talk.

But the answer there is basically, "if you care about payload, don't use zig's built-in error handling." Which, obviously, you can do, but I'm not convinced it's a great answer!

Even for something as simple as parseInt, it would be nice, I would argue, if it returned, along with InvalidCharacter, which character was invalid. This would enable e.g. very precise indications in messages to the end user of what was wrong with the input.

In general, because currently it's super-ergonomic to return just error codes but more of a pain to return errors w/ payloads, what you'll get in practice is no error payload even when it would be useful, which will end up turning into less-helpful-than-ideal errors for end users, which is contrary to zig's goal of enabling the creation of perfect software. :-)

I found this thread interesting

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

Re:Zig in 30 Minutes by jorangreef on Monday January 04, 2021 @09:54AM (#25632033)
Thanks for the link to that proposal.

Just thinking out loud, given Zig's errors primarily allow the compiler to enforce that errors are handled, and exhaustively, beyond that why can't error payloads just be passed as an input argument pointer to the function call? Maybe this is a dumb idea, but it's what C programs already do, except C programs don't have the safety of compiler checked errors offered by Zig. Does the Zig compiler need to check error payloads beyond the type system already provided? I'm not sure.

I agree with you that having such a super-ergonomic error handling system will direct most code towards a certain style, but I don't think this is contrary to Zig's goals of perfect software or simplicity. It's already such a huge win on top of C's error handling.

Re:Zig in 30 Minutes by dilap on Monday January 04, 2021 @06:34PM (#25639774)
> Just thinking out loud, given Zig's errors primarily allow the compiler to enforce that errors are handled, and exhaustively, beyond that why can't error payloads just be passed as an input argument pointer to the function call?

Yeah, that's definitely a reasonable way to do it, and actually Andrew gives an example of this:

https://github.com/ziglang/zig/issues/2647#issuecomment-5898...

(Andrew's example is slightly different in that rather than an explicit argument, it's an optional field in one of the arguments.)

So it's definitely not like crazy bad or anything, but also definitely less ergonomic than if you could directly return a payload with the error, and this is enough friction that it feels like you'll end up not having error information when you want it (both as an API consumer and eventually as an end-user looking at an error message).

But, I'm still very new to Zig, so perhaps my instincts on that are wrong. :-)

Re:Zig in 30 Minutes by jorangreef on Tuesday January 05, 2021 @06:25AM (#25644398)
> But, I'm still very new to Zig, so perhaps my instincts on that are wrong. :-)

Maybe you're right too! Just speaking for myself here, but as a programmer and "man with a hammer", I sometimes like to think that instincts might just be our mental machine learning model, where it can be really well trained and give the right black box answer, albeit not with the explanatory backstory or rationale, but worth trusting nevertheless.

I'm also new to Zig, and there are ways that a pointer input arg as error payload could go wrong. I'm following the proposal issue, and it will be interesting to see which way it goes.

Re:Zig in 30 Minutes by dilap on Tuesday January 05, 2021 @07:46PM (#25653437)
I think you're spot on with your description of instincts. Which of course means sometimes they can lead us wrong, especially if we're applying them to domains very different from those they were trained on.
Re:Zig in 30 Minutes by jeltz on Monday January 04, 2021 @01:35AM (#25628421)
To me the scope leak was annoying because how Zig does not allow shadowing, so I have to write:

  var it = a.getSomeIterable();
  while (it.next()) |item| {
      ...
  }

  var it2 = b.getSomeIterable();
  while (it2.next()) |item| {
      ...
  }
Re:Zig in 30 Minutes by Leherenn on Monday January 04, 2021 @04:33AM (#25629216)
You can do:

  {
    var it = a.getSomeIterable();
    while (it.next()) |item| {
        ...
    }
  }
  {
    var it = b.getSomeIterable();
    while (it.next()) |item| {
        ...
    }
  }
Re:Zig in 30 Minutes by Leherenn on Monday January 04, 2021 @04:36AM (#25629235)
Yeah, it's not that common in general, I agree. I think my impression was coloured by the fact I tried Zig to implement a binary protocol deserialiser where getting the size first then iterating happened a lot.
Re:Zig in 30 Minutes by AnIdiotOnTheNet on Monday January 04, 2021 @07:36AM (#25630520)
The pattern I use for that is:

  get size
  allocate slice
  for(slice) |*elem| elem.* = read()
Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @09:51AM (#25622059)
Both forms feature "implicit flow of control", contrary to Zig's stated goals. In "while (i < 10) : (i += 1)", it's hard to understand what the heck "(i += 1)". The best concept one might get is that it's puposely made be different from C, just to be different and confuse people.

In second case, it's "defer", coming from Go, the language which chickened out to add normal exceptions, because they're "implicit transfer of control", and LOLishly added "defer", as if it's not such.

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @09:58AM (#25622106)
> Both forms feature "implicit flow of control", contrary to Zig's stated goals.

They absolutely don't. All control paths are explicitly represented by syntax (no different from the hidden goto in a while loop -- it's explicitly recognised by the while syntax); proof: you can draw all of them by just examining the syntax of the current subroutine, while knowing nothing about others. Exceptions, however, are implicit: any call, foo(), might or might not cause some control flow change in the client without there being any explicit acknowledgement of that by the client; you cannot draw all the flow paths just by examining the syntax of the current subroutine.

Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @10:18AM (#25622217)
Sorry, but throwing an exception in a function call is equivalent to:

    res, exc = fun();
    if (exc) goto exception_handler;
That's underlying model of how exceptions behave, and how they're implemented "manually" in languages with no exception handling (C, Go). You absolutely can draw that by examining syntax of a subroutine, and it's no more implicit than "defer".
Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @10:32AM (#25622292)
Right, both the C style and Zig's defer are explicit, as opposed to exceptions, which are implicit, only Zig's error handling is less error-prone (it forces you to handle errors) and makes the code more readable, IMO, than the C style.
Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @10:48AM (#25622404)
But no, there's a kind of continuum, and Go/Zig "defer" is already pretty high towards "implicit flow of control" end. (I agree that exceptions are a notch higher.)

It's only C's syntax which is truly explicit. It's literally a structured machine-independent assembler. That's why it's gold language which is very hard to displace (it's already perfect for what it is). But just as everyone I'm watching with popcorn all the contenders popping up. (My humble opinion about Zig's issues on that path, I, together with other people, expressed here.)

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @11:01AM (#25622481)
> is already pretty high towards "implicit flow of control" end

Its implicitness is zero -- there is zero information not available in the syntax of the current block, exactly as in C -- so I don't see how it can be high compared to anything. You just don't have it in C, so you're not used to it. This is exactly like an Assembly programmer who says that C's `while` is implicit because there is no explicit jmp. In fact, the third clause in C's `for` header works almost exactly like defer: you write a piece of code that isn't executed immediately after the preceding one, but is injected to the end of the block.

A language with `while` isn't any more implicit than a language with just goto; it just has another explicit control-flow construct. Same goes for defer.

> It's literally a structured machine-independent assembler.

Not so much once you take the preprocessor into account.

> it's already perfect for what it is

I think that was true in 1970, not today. First, we know more. Second, we write much bigger programs. Third, "structured assembly" isn't as valuable as it was now that even machine code is "high level." This is not to say that C could be replaced everywhere, but I think, intertia aside, it could be replaced almost everywhere.

Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @11:33AM (#25622767)
> Its implicitness is zero -- there is zero information not available in the syntax of the current block

Ah, so your mind's window is single block, that what you bang on. You know, blocks can be long too. To the end of reading 1000-line block, you think that you know how it ends. But oops, you completely forgot about some "defer", which implicitly executes at the end of block. That's my definition of "true explicitness", where all code which executes at some point is available locally.

And if you forgo that definition of explicitness, why stop at block? Exceptions work across blocks, i.e. on the level of the entire subroutine, and not much harder to reason about than "defer".

> In fact, the third clause in C's `for` header works almost exactly like defer

Good point. It's a clause in "for", so best practice is to use it for "loop iteration expression". It's also familiar syntax to the entire generations of programmers. Unlike Zig's "2nd clause in while" which is "original design" people will "thank" you for.

> it could be replaced almost everywhere.

The question is with what. IMHO, Zig isn't good enough to replace C at all. Too much NIH, again. It does too much differently. Naive thinking is of course "so that it's better", but actually, it just repeats C's mistakes (ugliness in language) and makes its own, "very original" ones.

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @11:53AM (#25622907)
> That's my definition of "true explicitness", where all code which executes at some point is available locally.

But no control flow construct satisfies that. Jumps are the whole point, and the unit for structured control flow is the block (what's the next instruction after `break`, `continue` or `}`?). defer is just as structured, just as local, and just as explicit as C's control flow constructs.

> It's also familiar syntax to the entire generations of programmers.

True, but learning Zig takes a day; two tops. Where it differs in syntax from C, it does so for good reason, and it's not like it's a complex language where different syntax is an additional burden. C's `for` syntax just doesn't make sense (`;`, which everywhere else in the language denotes sequential composition, means something different in the `for` header). Is an additional 2 minutes of learning, in a language that has very little syntax, not worth fixing something that's unpleasant in C?

Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @09:45AM (#25622035)
> while (it.next()) |item| {

Talk about confusing, implicit syntax of Zig - there's no connection between "it.next()" and "|item|". It's implicit and you need to make it up. Compare that to other languages: "for item in it" or "while (item = it.next())".

Re:Zig in 30 Minutes by AnIdiotOnTheNet on Sunday January 03, 2021 @10:02AM (#25622124)
It isn't confusing. The meaning of the syntax isn't obvious if you didn't bother to learn zig syntax, but it is entirely consistent with how zig does this sort of thing.

The for loop payload syntax, where you can also give a name to the counter, is the only weirdness with it.

Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @10:11AM (#25622167)
I'm sorry, but I do programming languages for decades, and familiar with a bunch of them. There's absolutely nothing special about Zig, it's yet another wannabe proglingo. What sets it apart is strong desire "to replace C" while being apparently "as different as possible from it".

I understand such a strategy (among many possible), and that will be a fun strategy if it pans out. The current situation though is that people question: why that dude purposely make it harder than needed for everyone?

Re:Zig in 30 Minutes by pron on Sunday January 03, 2021 @02:29PM (#25624369)
> There's absolutely nothing special about Zig

That's just not true. It is not only pretty exceptional in its general partial evaluation construct combined with compile-time introspection (although D and Nim have some similar features), it is the only language AFAIK where a single general partial evaluation construct is used in stead of type generics, value generics, typeclasses/concepts, constexprs, macros and conditional compilation.

Zig might not be your cup of tea, and that's fine, but it is without a doubt unique in its design and not only special, but pretty radical.

> Talk about confusing, implicit syntax of Zig

I don't think it's any more confusing than any other syntax (and less confusing than C's `for` syntax where the ubiquitous `;` is treated differently than anywhere else), even though it might not be familiar to you before learning it, but there is absolutely nothing implicit here. You might find `in` more appealing to you or more familiar than `|` but it's certainly not more explicit.

Re:Zig in 30 Minutes by AnIdiotOnTheNet on Sunday January 03, 2021 @10:40AM (#25622350)
I feel like what you're complaining about is the equivelent of Calculon's critique of a screenplay: "no, I don't like the font". I am very much not a genius (check the user name) and it took me less than a minute to understand the payload operator (which IIRC was taken from Ruby or something).

Let's say `for (x in y) {}` replaced the payload operator. It would still exist in:

- if(some_error_union) |value| {} else |err| {}

- if(some_nullable) |value| {}

- while(some_nullable) |value| {}

- switch(some_tagged_union) { .some_tag => |value| {} }

And any that I might be forgetting. Is your suggestion that each of these should have separate syntax, thus complicating the language with one-off syntax?

Re:Zig in 30 Minutes by dragonwriter on Sunday January 03, 2021 @11:19AM (#25622643)
> I feel like what you're complaining about is the equivelent of Calculon's critique of a screenplay: "no, I don't like the font".

Syntax is not irrelevant to the main use of a programming language the way fonts are to the use someone is probably concerned with if they ask an opinion of a screenplay, so, no, I don't see a similarity there.

> it took me less than a minute to understand the payload operator (which IIRC was taken from Ruby or something).

Loosely inspired by Ruby, perhaps, but definitely not taken from. Neither the positioning nor the semantics are the same.

Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @10:53AM (#25622433)
> I feel like what you're complaining about is the equivelent of Calculon's critique of a screenplay: "no, I don't like the font"

I'm a human, and syntax is very important to me. 't broken read can syntax I. In that regard, if you purposely (or naively) misuse Comic Sans, then yes, "I don't like the font".

> (which IIRC was taken from Ruby or something).

Bingo. Ruby's not everyone's favorite language, so please be ready that people will send you rays of some hard love in that regard. Did you consider using C syntax for C-replacement-wannabe? (Rhetoric question.)

Re:Zig in 30 Minutes by AnIdiotOnTheNet on Sunday January 03, 2021 @11:06AM (#25622527)
From what you're saying, I believe you would complain about a foreign language simply because it doesn't resemble your native language enough. Zig's syntax choices are not arbitrary and great effort is made to ensure their consistency. Your complaints about the syntax aren't that there is anything inconsistent or illogical about the syntax, just that it isn't the syntax you're used to.

That's a valid reason to not want to use the language, but I don't believe it is a valid basis for criticism and certainly doesn't excuse the hostility of your comments in this thread.

Re:Zig in 30 Minutes by pfalcon on Sunday January 03, 2021 @12:19PM (#25623127)
> From what you're saying, I believe you would complain about a foreign language simply because it doesn't resemble your native language enough.

I assume you mean a natural language. No, I won't complain about a real language used by people. I might criticize someone's inventing their own natural language. The same as I do for programming languages. And I'm very well familiar with the "we need to invent to move forward" argument. I also assume that people taking that ungrateful task are aware that they will receive criticism for that.

> just that it isn't the syntax you're used to.

Not just me, but many people. I specifically started to comment, seeing other people's feedback resonates with my own sentiment about Zig.

> certainly doesn't excuse the hostility of your comments in this thread.

I don't think there was particular "hostility", but I indeed try to nail right at the spot of the issues. Ouch! Beyond that, I allowed me some sour comments regarding downvoting other peoples' and my comments where concerns were raised with way Zig does some things. Such downvoting could be considered hostility, but I actually consider it funny ;-).

Re:Zig in 30 Minutes by AnIdiotOnTheNet on Sunday January 03, 2021 @02:10PM (#25624160)
If you go into a conlang discussion and complain about people inventing languages when there's already natural languages, I guarantee you will get downvoted and pretty much for the same reason.

Let's look at some of your criticisms:

  > @call(.{ .modifier = .never_inline }, collectNoInline, .{gc});
> 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);
In which you say that a built-in function call with optional flags and settings is total nonsense, while hijacking comment syntax to change compiler behavior is better. You can make an argument for that, but the way you describe it as "total nonsense" and not in good faith is definitely hostile.

Let's look at some more examples:

> Jumping thru hoops is what Zig seems to choose with its unorthodox, different-just-to-be-different syntax.

> Zig appears to have strong NIH mentality in regard to the syntax. Trying to replace C, but go out of their way trying to invent syntax just to be different from it and most other things too.

Zig's syntax isn't that unorthodox except compared to languages that look even more like C than it does, and it definitely isn't different just to be different. By claiming such, when there are thousands of posts in discussions about syntax, is plain insulting. Can you provide even a single post that suggests anyone has ever suggested syntax be a certain way just to be different?