Skip to content

Commit

Permalink
Add Optional.modify overload
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stephencelis committed Feb 5, 2025
1 parent 19b7263 commit dc18e34
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sources/CasePathsCore/Optional+CasePathable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,24 @@ extension Optional where Wrapped: CasePathable {
public func `is`(_ keyPath: PartialCaseKeyPath<Wrapped>) -> Bool {
self?[case: keyPath] != nil
}

@_disfavoredOverload
@_documentation(visibility: internal)
public mutating func modify<Value>(
_ keyPath: CaseKeyPath<Wrapped, Value>,
yield: (inout Value) -> Void,
fileID: StaticString = #fileID,
filePath: StaticString = #filePath,
line: UInt = #line,
column: UInt = #column
) {
modify(
(\Cases.some).appending(path: keyPath),
yield: yield,
fileID: fileID,
filePath: filePath,
line: line,
column: column
)
}
}

0 comments on commit dc18e34

Please sign in to comment.