for arr: (it) {}
This commit is contained in:
25
specs.md
25
specs.md
@@ -789,22 +789,23 @@ while i < 10 {
|
||||
|
||||
### For Loop
|
||||
```sx
|
||||
for iterable {
|
||||
// `it` is the current element
|
||||
// `it_index` is the current index (s64)
|
||||
print("{it}\n");
|
||||
}
|
||||
for iterable: (elem) { } // element alias (no copy)
|
||||
for iterable: (elem, ix) { } // element + index
|
||||
for iterable: (_, ix) { } // index only
|
||||
```
|
||||
Iterates over arrays and slices. The loop body has two implicit variables:
|
||||
- `it` — the current element value
|
||||
- `it_index` — the current index (s64, starting at 0)
|
||||
Iterates over arrays and slices. The capture clause after `:` binds loop variables:
|
||||
- The first name is the element capture (non-reassignable alias into the array/slice)
|
||||
- The optional second name is the index (s64, starting at 0, also non-reassignable)
|
||||
- Use `_` to discard a capture
|
||||
|
||||
The element capture is a direct alias — reads and field writes go to the original array element. Direct reassignment of the capture (`elem = x`) is a compile error.
|
||||
|
||||
`break;` exits the loop. `continue;` skips to the next iteration.
|
||||
```sx
|
||||
arr : [5]s32 = .[1, 2, 3, 4, 5];
|
||||
for arr {
|
||||
if it_index == 2 { continue; }
|
||||
print("{it}\n");
|
||||
for arr: (val, ix) {
|
||||
if ix == 2 { continue; }
|
||||
print("{}\n", val);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1069,7 +1070,7 @@ assignment = lvalue ('=' | '+=' | '-=' | '*=' | '/=') expr
|
||||
lvalue = IDENT | postfix '.' IDENT
|
||||
expr = if_expr | match_expr | while_expr | for_expr | lambda | binary
|
||||
while_expr = 'while' expr block
|
||||
for_expr = 'for' expr block
|
||||
for_expr = 'for' expr ':' '(' IDENT [',' IDENT] ')' block
|
||||
binary = unary (binop unary)*
|
||||
unary = ('-' | '!' | 'xx' | 'cast' '(' type ')') postfix
|
||||
| postfix
|
||||
|
||||
Reference in New Issue
Block a user