Skip to content

Commit 07b58ed

Browse files
simanerushamartini51
authored andcommitted
Add a paragraph about pack iteration
Cherry-picked from #206
1 parent 90aa68d commit 07b58ed

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

TSPL.docc/ReferenceManual/Statements.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ and a `continue` statement and is discussed in <doc:Statements#Break-Statement>
7070
A `for`-`in` statement allows a block of code to be executed
7171
once for each item in a collection (or any type)
7272
that conforms to the
73-
[`Sequence`](https://developer.apple.com/documentation/swift/sequence) protocol.
73+
[`Sequence`](https://developer.apple.com/documentation/swift/sequence) protocol, or in a value parameter pack.
7474

7575
A `for`-`in` statement has the following form:
7676

7777
```swift
78-
for <#item#> in <#collection#> {
78+
for <#item#> in <#expression#> {
7979
<#statements#>
8080
}
8181
```
8282

83-
The `makeIterator()` method is called on the *collection* expression
84-
to obtain a value of an iterator type --- that is,
85-
a type that conforms to the
83+
If the *expression* of a `for`-`in` statement is a collection *expression*, the
84+
`makeIterator()` method is called on it to obtain a value of an iterator type
85+
--- that is, a type that conforms to the
8686
[`IteratorProtocol`](https://developer.apple.com/documentation/swift/iteratorprotocol) protocol.
8787
The program begins executing a loop
8888
by calling the `next()` method on the iterator.
@@ -93,6 +93,16 @@ and then continues execution at the beginning of the loop.
9393
Otherwise, the program doesn't perform assignment or execute the *statements*,
9494
and it's finished executing the `for`-`in` statement.
9595

96+
The *expression* of a `for`-`in` statement may also be a pack expansion
97+
*expression*. In this case, on the *i*th iteration, the type of *item* is the
98+
*i*th type parameter in the type parameter pack being iterated over. The value
99+
of *item* is the pattern type of *expression*, with each captured type
100+
parameter pack replaced with an implicit scalar type parameter with matching
101+
requirements. The *expression* is evaluated once at each iteration, instead of
102+
`n` times eagerly, where `n` is the length of the packs captured by the
103+
pattern. The program will continue executing *statements* while the total
104+
length of the packs captured by the pattern isn't reached.
105+
96106
> Grammar of a for-in statement:
97107
>
98108
> *for-in-statement***`for`** **`case`**_?_ *pattern* **`in`** *expression* *where-clause*_?_ *code-block*

0 commit comments

Comments
 (0)