You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _data/authors.yml
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -421,10 +421,10 @@ rcollobert:
421
421
github: andresy
422
422
423
423
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."
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:
48
48
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`.
51
51
- 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.
52
52
53
53
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.
69
69
It turns out that there is just no concise way of implementing the function prior to Swift 6.0.
70
70
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:
@@ -78,7 +78,7 @@ func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each
78
78
if left == right {
79
79
return
80
80
}
81
-
81
+
82
82
throwNotEqual()
83
83
}
84
84
@@ -96,11 +96,11 @@ func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each
96
96
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.
97
97
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.
98
98
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`.
100
100
101
101
## Introducing Pack Iteration
102
102
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.
104
104
105
105
More specifically, with pack iteration, the body of the `==` tuple comparison operator simplifies down to a simple `for`-`in repeat` loop:
106
106
@@ -114,7 +114,7 @@ func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each
114
114
}
115
115
```
116
116
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.
118
118
119
119
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.
120
120
At every iteration, the loop binds each element of the value parameter pack to a local variable.
@@ -162,11 +162,11 @@ protocol ValueProducer {
162
162
}
163
163
```
164
164
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.
166
166
167
167
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.
168
168
Also, suppose you need to save the result of each call into an array.
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