by dbandstra · 5y ago · 0 comments
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.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.
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).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.
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.
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.
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())".
The for loop payload syntax, where you can also give a name to the counter, is the only weirdness with 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?
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.
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?