remove_in_for
Finds removing from an array while a for walks it forward.
Category correctness, deny by default.
#import "Basic";
main :: () {
xs: [..] int;
array_add(*xs, 1, 2, 2, 3);
for xs {
if it == 2 array_unordered_remove_by_index(*xs, it_index);
}
print("%\n", xs);
}error[remove_in_for]: `array_unordered_remove_by_index` changes `xs` while this `for` walks it
--> example.jai:9:20
|
8 | for xs {
| -- this collection's cursor advances after removal
9 | if it == 2 array_unordered_remove_by_index(*xs, it_index);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: the element moved into the freed slot is skipped; use `remove it;` (or loop backwards with `for < xs`)
|
9 ~ if it == 2 remove it;
|
= note: `remove_in_for` is `deny` by default#import "Basic";
main :: () {
xs: [..] int;
array_add(*xs, 1, 2, 2, 3);
for xs {
if it == 2 remove it;
}
print("%\n", xs);
}The loop steps past the element moved into the removed slot. Fires when a removal at the cursor reaches the loop's step on every path; break, return or writing the index yourself stops it. Removal inferred through a wrapper counts.