Skip to content

Commit bf90fbe

Browse files
authored
Add Optional.modify overload (#216)
Optional chaining into `modify` can lead to quietly losing issues when you intend to modify something. Let's take advantage of the trick introduced in #204 to avoid losing the result via optional chaining.
1 parent 19b7263 commit bf90fbe

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Sources/CasePathsCore/Optional+CasePathable.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,24 @@ extension Optional where Wrapped: CasePathable {
7979
public func `is`(_ keyPath: PartialCaseKeyPath<Wrapped>) -> Bool {
8080
self?[case: keyPath] != nil
8181
}
82+
83+
@_disfavoredOverload
84+
@_documentation(visibility: internal)
85+
public mutating func modify<Value>(
86+
_ keyPath: CaseKeyPath<Wrapped, Value>,
87+
yield: (inout Value) -> Void,
88+
fileID: StaticString = #fileID,
89+
filePath: StaticString = #filePath,
90+
line: UInt = #line,
91+
column: UInt = #column
92+
) {
93+
modify(
94+
(\Cases.some).appending(path: keyPath),
95+
yield: yield,
96+
fileID: fileID,
97+
filePath: filePath,
98+
line: line,
99+
column: column
100+
)
101+
}
82102
}

0 commit comments

Comments
 (0)