lang: inline for element form over packs — multi-iterable parity
This commit is contained in:
12
specs.md
12
specs.md
@@ -1343,6 +1343,8 @@ may do, regardless of the concrete arg types at any particular call site.
|
||||
| Length | `xs.len` | comptime int (field-style, not `len(xs)`) |
|
||||
| Index | `xs[i]` | i-th element; `i` must be comptime |
|
||||
| Comptime unroll (index) | `inline for 0..xs.len (i) { ... }` | unrolled loop; cursor `i` is a comptime constant per iteration; not `#for` |
|
||||
| Comptime unroll (element) | `inline for xs (x) { ... }` | unrolled loop; `x` is the concrete i-th element, viewed through the constraint protocol (≡ `xs[i]`) |
|
||||
| Comptime unroll (element + index) | `inline for xs, 0.. (x, i) { ... }` | multi-iterable parity with the runtime `for`: position 0 drives the count, a trailing open range pairs the cursor |
|
||||
| Projection | `xs.field` | see "Pack projection" |
|
||||
| Spread → call args | `..xs` / `..xs.field` | expands to N positional args |
|
||||
| Spread → tuple value | `(..xs)` / `(..xs.field)` | materializes a tuple |
|
||||
@@ -1395,8 +1397,9 @@ suggestion:
|
||||
variadic `..xs: []P` (a runtime slice) instead of a pack `..xs: P`;
|
||||
- returning it (`return xs;`) → return a tuple `(..xs)` (and make the return
|
||||
type that tuple);
|
||||
- iterating it (`for xs (x)`, `xs[runtime_i]`) → `inline for 0..xs.len (i)`
|
||||
for a comptime unroll, or take `..xs: []P` for a runtime loop.
|
||||
- iterating it (`for xs (x)`, `xs[runtime_i]`) → `inline for xs (x)` (or
|
||||
`inline for 0..xs.len (i)` for the index) for a comptime unroll, or take
|
||||
`..xs: []P` for a runtime loop.
|
||||
|
||||
The recurring runtime escape hatch is the **slice-of-protocol variadic**
|
||||
`..xs: []P` (see "Variadic Functions"): it is the runtime, protocol-erased
|
||||
@@ -2093,7 +2096,10 @@ for xs, ys (x, y) { } // parallel (zip) iteration
|
||||
for 1..=5, 0.. (a, b) { } // a: 1..5, b: 0..4 (end inferred)
|
||||
for a4, b4, 100.. (p, q, k) { } // any number of positions
|
||||
for xs (x) => sum += x; // arrow body
|
||||
inline for 0..n (i) { } // comptime-unrolled single bounded range
|
||||
inline for 0..n (i) { } // comptime unroll; first range bounded
|
||||
inline for xs, 0.. (x, i) { } // comptime unroll over a PACK: x = the
|
||||
// concrete i-th element (see "Variadic
|
||||
// Heterogeneous Type Packs")
|
||||
```
|
||||
|
||||
**Range bound markers.** Each side of `..` takes an optional marker — `=`
|
||||
|
||||
Reference in New Issue
Block a user