#Does while loop support counter?
1 messages · Page 1 of 1 (latest)
I think to reduce the cognitive load, it should eventually get supported on all loops or removed. having loops with different set of Good feature is hard to track. the features are nice 👍 but they need to be harmonized accross IHMO
All languages have multiple ways
this would be excess of simplification for excess of simplification
there is no counter in the while because it makes no sense
is not a "All is done this way" it just doesn't make sense in the underlying concept
what the hell it means 0.. in this case
in the case of a for loop, ez, is the index of the current item
but in the case of the optional while pattern is just nothing
doesn't exists
is an arbitrary even possible to be eternal condition
is not running in a memory limit, it's running in arbitrary limit
@inland marlin Well that's a lot of reasons and I can only agree with them. But given from one loop to the other you have many ramifications just makes the thing more complex for the brain. In most cases the C way of looping is all we need. I just started learning Zig so it's just the way I am feeling right now.
if it's available on for the intuition is to also be on while that's my beginer level frustration 🙂
That's fine
just that they are different in semantisc
for -> this range of data
while -> this happens
I am not also comparing Zig with any language , it because of the difference I came here in the first place. to sens a bit of new fresh air
Oh, no, I am the one comparing
lel
Thanks for clarifying. semantically you are right. the for is more for range feature.
It's important to note that for (0..) |_| {} is a compile error, since as rimu says, for is meant to iterate a finite range of consecutive data, wheread while loops can have a condition that is never false, or iterate over an unbounded and non-sequential sequence of data
If you want to assert that an iterator returns a bounded number of items (ie, an amount of memory that wouldn't theoretically occupy the entire address space), you may instead choose to do
for (0..limit) |i| {
_ = i;
const item = iter.next() orelse break;
_ = item;
} else if (iter.next() != null) {
return error.Overflow;
}
(that's probably how NASA would use iterators in zig)
Small safety things:
for (0..limit) |i| {
_ = i;
const item = iter.next() orelse break;
_ = item;
} else if (!iter.hasNext()) {
return error.Overflow;
}
``` lmao
=V
jk
No, this returns overflow on the case next is null
=V
Nothing backwards, just a joke