#Does while loop support counter?

1 messages · Page 1 of 1 (latest)

autumn wing
#

I was expecting this to work the way it does on for loop.

while(iter.next(), 0..) |item, counter| { ... }
twilit meadow
#

no, not how while works

#

the range capture is specific to for loops

autumn wing
#

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

inland marlin
#

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

autumn wing
#

@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 🙂

inland marlin
#

That's fine

#

just that they are different in semantisc

#

for -> this range of data
while -> this happens

autumn wing
#

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

inland marlin
#

lel

autumn wing
#

Thanks for clarifying. semantically you are right. the for is more for range feature.

twilit meadow
#

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)

inland marlin
#

=V

#

jk

twilit meadow
#

Ye fair

#

lol

#

Also backwards condition

inland marlin
#

=V

#

Nothing backwards, just a joke

twilit meadow
#

oh pf

#

I'm tired

inland marlin
#

dw, I am joking in a question, no problme

#

xD

twilit meadow
#

lol, nah, is funny joke

#

I just ran 10km on treadmill so I'm fried