Skip to content

Commit 74a17cd

Browse files
authored
[TF] Add Optional: KeyPathIterable conformance. (#33992)
Add `KeyPathIterable` conformances for `Optional` and `Optional.TangentVector`.
1 parent be5c241 commit 74a17cd

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

stdlib/public/core/KeyPathIterable.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,25 @@ extension Dictionary: KeyPathIterable {
149149
return result
150150
}
151151
}
152+
153+
extension Optional: KeyPathIterable {
154+
public typealias AllKeyPaths = [PartialKeyPath<Self>]
155+
156+
public var allKeyPaths: [PartialKeyPath<Self>] {
157+
if self != nil {
158+
return [\.!]
159+
}
160+
return []
161+
}
162+
}
163+
164+
extension Optional.TangentVector: KeyPathIterable {
165+
public typealias AllKeyPaths = [PartialKeyPath<Self>]
166+
167+
public var allKeyPaths: [PartialKeyPath<Self>] {
168+
if value != nil {
169+
return [\Self.value!]
170+
}
171+
return []
172+
}
173+
}

test/stdlib/KeyPathIterable.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct Nested : KeyPathIterable, Equatable {
3333
struct ComplexNested : KeyPathIterable, Equatable {
3434
var float: Float
3535
let simple: Simple
36+
let optional: Simple?
3637
let array: [Simple]
3738
var dictionary: [String : Simple]
3839
}
@@ -125,16 +126,22 @@ KeyPathIterableTests.test("SimpleNested") {
125126

126127
KeyPathIterableTests.test("ComplexNested") {
127128
var x = ComplexNested(float: 1, simple: Simple(w: 3, b: 4),
129+
optional: Simple(w: 5, b: 6),
128130
array: [Simple(w: 5, b: 6), Simple(w: 7, b: 8)],
129131
dictionary: ["foo" : Simple(w: 1, b: 2),
130132
"bar" : Simple(w: 3, b: 4)])
131133
expectEqual([\ComplexNested.float, \ComplexNested.simple,
132-
\ComplexNested.array, \ComplexNested.dictionary],
134+
\ComplexNested.optional, \ComplexNested.array,
135+
\ComplexNested.dictionary],
133136
x.allKeyPaths)
134137
expectEqual([\ComplexNested.float,
135138
\ComplexNested.simple,
136139
\ComplexNested.simple.w,
137140
\ComplexNested.simple.b,
141+
\ComplexNested.optional,
142+
\ComplexNested.optional!,
143+
\ComplexNested.optional!.w,
144+
\ComplexNested.optional!.b,
138145
\ComplexNested.array,
139146
\ComplexNested.array[0],
140147
\ComplexNested.array[0].w,
@@ -153,6 +160,8 @@ KeyPathIterableTests.test("ComplexNested") {
153160
expectEqual([\ComplexNested.float,
154161
\ComplexNested.simple.w,
155162
\ComplexNested.simple.b,
163+
\ComplexNested.optional!.w,
164+
\ComplexNested.optional!.b,
156165
\ComplexNested.array[0].w,
157166
\ComplexNested.array[0].b,
158167
\ComplexNested.array[1].w,
@@ -175,6 +184,7 @@ KeyPathIterableTests.test("ComplexNested") {
175184
}
176185
// Check that recursively all `Float` properties have been mutated.
177186
let expected = ComplexNested(float: 2, simple: Simple(w: 3, b: 4),
187+
optional: Simple(w: 5, b: 6),
178188
array: [Simple(w: 5, b: 6), Simple(w: 7, b: 8)],
179189
dictionary: ["foo" : Simple(w: 2, b: 3),
180190
"bar" : Simple(w: 4, b: 5)])

0 commit comments

Comments
 (0)