#What's the simplest way to make a for loop for x amount of iterations?
1 messages · Page 1 of 1 (latest)
that's the only proper way at the moment
at some point we will have syntax for for (n..m) |i| though
in the meantime, there's also a cursed version
fn voidArr() [*]void {
return undefined;
}
test {
for (voidArr()[0..amount]) |_, index| {
_ = index;
}
}
I don't recommend using it
but I also don't not recommend using it
oof, okay.. thanks. i was really hoping there would be a simpler version for this, since it's one of the most common things to do, right?
is this definitely planned? would be quite a relief 🙂
yes, quite so
though, for a long time the consensus
was that it wasn't
it only changed within the last year because it was accepted as a side-feature for another one, which it elevates
i see. is there a github issue that tracks the discussion?
otherwise, the general sentiment was that var i: usize = 0; while (i < x) : (i += 1) {...} was good enough
for (a...b) |x, index| { ... } Where a and b can be chars, integers, anything that can define a range. This is also better syntax IMHO than: {var i = 0; while (i < n; i += 1) { ... }}
these are the two relevant issues
the first one is technically rejected
but it is in a way, un-rejected in the subsequent issue, during some design bikeshedding
my main issue with the current way is not only that it's verbose but that i have to manually reset my index variable AFTER the loop
(if i want to do e.g. another loop right after)
yes, yes, I know, this is a tired conversation
there's lots of arguments for and against
point is though, that it's a settled matter
okay, thanks. will read those issues then 🙏
oh wow, so it looks like this syntax would become possible then
for (0..10) |i| {
// ...
}
seems super clean, love it! ❤️
Until then though, you could simply use any array with a larger length than your number of repeats and do a for loop on a slice of that array...
for (array[0..5]) |_| {
// ...
}
Or is that improper?
that will issue an out of bounds error, unless you do something like what I demo'd before, with a void many item pointer. That's safe since loads and stores on 0-sized values never actually generate code
or wait, you mean with an array that's large enough
I mean, that's a solution, but it certainly leaves much to be desired
you end up allocating a bunch of stack or static memory just to do a loop, and you're constrained to the bounds of that particular array, meaning if you have a dynamic bound you can never be certain the program will run correctly
the needless allocation issue is solved by using a 0-sized item like void, and then you can solve the bounds problem by just not having one in the first place, with a many item pointer
Thanks for explaining, if you have a large array anyway, would this still cause allocation issues?
I mean, if you already happen to have a large array, then that's not an allocation issue, that's just already having allocated a lot of memory. The issue I pointed out was that doing so just to do a ranged for loop is very wasteful.
Good, I did think of that when I said can use any array with larger length. Thanks for clarifying!
I think you could also use a manyitem pointer: [*]u8 in stead of an array? Any problems with that?
that will generate actual loads and stores in the output, which will ultimately segfault if it's accessing memory outside of the bounds defined by the OS - or just do other very bad things like delete your OS, because it's be undefined behaviour, which can amount to literally anything (not likely to delete your hard drive, but it's not defined as not being able to do that)