Skip to content

Commit ade2af7

Browse files
authored
Fix a typo in the Pack Iteration post and update authors.yml. (#1144)
1 parent 06c7869 commit ade2af7

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

_data/authors.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ rcollobert:
421421
github: andresy
422422

423423
simanerush:
424-
name: Sima Nerush
425-
email: simanerush@gmail.com
426-
github: simanerush
427-
about: "Sima Nerush is an incoming engineer on the Apple SwiftUI team. She is an experienced iOS Engineer, Swift Compiler contributor, and a computer science student."
424+
name: Sima Nerush
425+
email: sima_nerush@apple.com
426+
github: simanerush
427+
about: "Sima Nerush is a member of the SwiftUI team at Apple, and a Swift compiler contributor."
428428

429429
rauhul:
430430
name: Rauhul Varma

_posts/2024-03-07-pack-iteration.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ func == <A, B, C>(lhs: (A, B, C), rhs: (A, B, C)) -> Bool where A: Equatable, B:
3333

3434
// and so on, up to 6-element tuples
3535
```
36-
In each of the generic functions above, every element of the input tuple has to have its type declared in the generic parameter list of the function.
37-
Thus, we need to add a new element to the generic parameter list any time we want to support a larger tuple size.
38-
Because of this, the artificial limit of 6-element tuples was imposed.
36+
In each of the generic functions above, every element of the input tuple has to have its type declared in the generic parameter list of the function.
37+
Thus, we need to add a new element to the generic parameter list any time we want to support a larger tuple size.
38+
Because of this, the artificial limit of 6-element tuples was imposed.
3939

40-
Parameter packs added the ability to abstract a function over a variable number of type parameters.
40+
Parameter packs added the ability to abstract a function over a variable number of type parameters.
4141
This means that we can lift the 6-element limit using an `==` operator written like this:
4242

4343
```swift
4444
func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each Element)) -> Bool
4545
```
4646

47-
Let's break down the types we see in the above signature:
47+
Let's break down the types we see in the above signature:
4848

49-
- Note `each Element` in the list of generic parameters. The `each` keyword indicates that `Element` is a *type parameter pack*, meaning that it can accept any number of generic arguments. Just like with non-pack (*scalar*) generic parameters, we can declare a conformance requirement on the type parameter pack. In this case, we require each `Element` type to conform to the `Equatable` protocol.
50-
- This function takes in two tuples, `lhs` and `rhs`, as arguments. In both cases, the tuple's element type is `repeat each Element`. This is called the *pack expansion type*, which consists of a `repeat` keyword followed by a *repetition pattern*, which has to contain a pack reference. In our case, the repetition pattern is `each Element`.
49+
- Note `each Element` in the list of generic parameters. The `each` keyword indicates that `Element` is a *type parameter pack*, meaning that it can accept any number of generic arguments. Just like with non-pack (*scalar*) generic parameters, we can declare a conformance requirement on the type parameter pack. In this case, we require each `Element` type to conform to the `Equatable` protocol.
50+
- This function takes in two tuples, `lhs` and `rhs`, as arguments. In both cases, the tuple's element type is `repeat each Element`. This is called the *pack expansion type*, which consists of a `repeat` keyword followed by a *repetition pattern*, which has to contain a pack reference. In our case, the repetition pattern is `each Element`.
5151
- At the call site, the user provides *value parameter packs* for each tuple that will be substituted into their corresponding type parameter packs. At runtime, the repetition pattern will be repeated for each element in the substituted pack.
5252

5353
With the tuple equality operator implemented using parameter packs, let's look at the call site again to understand these concepts better.
@@ -69,7 +69,7 @@ Feel free to take a moment to think about this.
6969
It turns out that there is just no concise way of implementing the function prior to Swift 6.0.
7070
One solution involves creating a local function that compares a pair of elements from the two tuples, and then using pack expansion to call that function for every pair of elements, like this:
7171

72-
```swift
72+
```swift
7373
struct NotEqual: Error {}
7474

7575
func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each Element)) -> Bool {
@@ -78,7 +78,7 @@ func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each
7878
if left == right {
7979
return
8080
}
81-
81+
8282
throw NotEqual()
8383
}
8484

@@ -96,11 +96,11 @@ func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each
9696
The above code doesn't look great, right? To simply check a condition for each pair of elements, we need to declare a local function `isEqual`, that just compares the given elements.
9797
However, this is not enough to make the function return early since the local `isEqual` function will still be called on every pair of elements in the parameter packs `lhs` and `rhs` when expanding them.
9898
Because of this, `isEqual` has to be marked `throws` and throw an error once a pair of mismatched elements is found.
99-
Then, we catch the error in a `catch` block to return `false`.
99+
Then, we catch the error in a `catch` block to return `false`.
100100

101101
## Introducing Pack Iteration
102102

103-
Swift 6.0 greatly simplifies this task with the introduction of pack iteration using the familiar `for`-`in` loop syntax.
103+
Swift 6.0 greatly simplifies this task with the introduction of pack iteration using the familiar `for`-`in` loop syntax.
104104

105105
More specifically, with pack iteration, the body of the `==` tuple comparison operator simplifies down to a simple `for`-`in repeat` loop:
106106

@@ -114,7 +114,7 @@ func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each
114114
}
115115
```
116116

117-
In the above code, we are able to utilize the `for`-`in` loop capability to iterate over the tuples pairwise.
117+
In the above code, we are able to utilize the `for`-`in` loop capability to iterate over the tuples pairwise.
118118

119119
Note that when iterating over packs, we use the new `for`-`in repeat` syntax, followed by a value parameter pack that we are iterating over.
120120
At every iteration, the loop binds each element of the value parameter pack to a local variable.
@@ -162,11 +162,11 @@ protocol ValueProducer {
162162
}
163163
```
164164

165-
The above protocol `ValueProducer` requires the `evaluate()` method that's return type is the associated type `Value` that conforms to `Codable` protocol.
165+
The above protocol `ValueProducer` requires the `evaluate()` method that's return type is the associated type `Value` that conforms to `Codable` protocol.
166166

167167
Suppose you get a parameter pack of values of type `Result<ValueProducer, Error>`, and you need to iterate only over the `success` elements and call the `evaluate()` method on its value.
168168
Also, suppose you need to save the result of each call into an array.
169-
Pack iteration makes this task super easy!
169+
Pack iteration makes this task super easy!
170170

171171
```swift
172172
func evaluateAll<each V: ValueProducer, each E: Error>(result: repeat Result<each V, each E>) -> [any Codable] {
@@ -221,7 +221,7 @@ struct BoolProducer: ValueProducer {
221221
struct SomeError: Error {}
222222

223223
print(evaluateAll(result:
224-
Result<SomeValueProducer, SomeError>.success(SomeValueProducer(5)),
224+
Result<IntProducer, SomeError>.success(IntProducer(5)),
225225
Result<SomeValueProducer, SomeError>.failure(SomeError()),
226226
Result<BoolProducer, SomeError>.success(BoolProducer(true))))
227227

@@ -231,4 +231,4 @@ print(evaluateAll(result:
231231
## Summary
232232

233233
We are excited to bring pack iteration to Swift 6.0!
234-
As seen in this article, pack iteration makes interacting with value parameter packs significantly more straightforward, making such an advanced feature more accessible and intuitive to incorporate into your Swift code.
234+
As seen in this article, pack iteration makes interacting with value parameter packs significantly more straightforward, making such an advanced feature more accessible and intuitive to incorporate into your Swift code.

0 commit comments

Comments
 (0)