Skip to content

[TF] Add Optional: KeyPathIterable conformance. #33992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions stdlib/public/core/KeyPathIterable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,25 @@ extension Dictionary: KeyPathIterable {
return result
}
}

extension Optional: KeyPathIterable {
public typealias AllKeyPaths = [PartialKeyPath<Self>]

public var allKeyPaths: [PartialKeyPath<Self>] {
if self != nil {
return [\.!]
}
return []
}
}

extension Optional.TangentVector: KeyPathIterable {
public typealias AllKeyPaths = [PartialKeyPath<Self>]

public var allKeyPaths: [PartialKeyPath<Self>] {
if value != nil {
return [\Self.value!]
}
return []
}
}
12 changes: 11 additions & 1 deletion test/stdlib/KeyPathIterable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct Nested : KeyPathIterable, Equatable {
struct ComplexNested : KeyPathIterable, Equatable {
var float: Float
let simple: Simple
let optional: Simple?
let array: [Simple]
var dictionary: [String : Simple]
}
Expand Down Expand Up @@ -125,16 +126,22 @@ KeyPathIterableTests.test("SimpleNested") {

KeyPathIterableTests.test("ComplexNested") {
var x = ComplexNested(float: 1, simple: Simple(w: 3, b: 4),
optional: Simple(w: 5, b: 6),
array: [Simple(w: 5, b: 6), Simple(w: 7, b: 8)],
dictionary: ["foo" : Simple(w: 1, b: 2),
"bar" : Simple(w: 3, b: 4)])
expectEqual([\ComplexNested.float, \ComplexNested.simple,
\ComplexNested.array, \ComplexNested.dictionary],
\ComplexNested.optional, \ComplexNested.array,
\ComplexNested.dictionary],
x.allKeyPaths)
expectEqual([\ComplexNested.float,
\ComplexNested.simple,
\ComplexNested.simple.w,
\ComplexNested.simple.b,
\ComplexNested.optional,
\ComplexNested.optional!,
\ComplexNested.optional!.w,
\ComplexNested.optional!.b,
\ComplexNested.array,
\ComplexNested.array[0],
\ComplexNested.array[0].w,
Expand All @@ -153,6 +160,8 @@ KeyPathIterableTests.test("ComplexNested") {
expectEqual([\ComplexNested.float,
\ComplexNested.simple.w,
\ComplexNested.simple.b,
\ComplexNested.optional!.w,
\ComplexNested.optional!.b,
\ComplexNested.array[0].w,
\ComplexNested.array[0].b,
\ComplexNested.array[1].w,
Expand All @@ -175,6 +184,7 @@ KeyPathIterableTests.test("ComplexNested") {
}
// Check that recursively all `Float` properties have been mutated.
let expected = ComplexNested(float: 2, simple: Simple(w: 3, b: 4),
optional: Simple(w: 5, b: 6),
array: [Simple(w: 5, b: 6), Simple(w: 7, b: 8)],
dictionary: ["foo" : Simple(w: 2, b: 3),
"bar" : Simple(w: 4, b: 5)])
Expand Down