Skip to content

Commit 8cc3bc0

Browse files
authored
Open existentials sent to PartialCaseKeyPath.callAsFunction (#145)
1 parent 76d7791 commit 8cc3bc0

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Sources/CasePaths/CasePathable.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,14 @@ extension PartialCaseKeyPath {
279279
/// type, the operation will fail.
280280
/// - Returns: An enum for the case of this key path that holds the given value, or `nil`.
281281
@_disfavoredOverload
282-
public func callAsFunction<Enum: CasePathable, AnyAssociatedValue>(
283-
_ value: AnyAssociatedValue
282+
public func callAsFunction<Enum: CasePathable>(
283+
_ value: Any
284284
) -> Enum?
285285
where Root == Case<Enum> {
286-
(Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue>)?.embed(value) as? Enum
286+
func open<AnyAssociatedValue>(_ value: AnyAssociatedValue) -> Enum? {
287+
(Case<Enum>()[keyPath: self] as? Case<AnyAssociatedValue>)?.embed(value) as? Enum
288+
}
289+
return _openExistential(value, do: open)
287290
}
288291
}
289292

Tests/CasePathsTests/CasePathsTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,30 @@ final class CasePathsTests: XCTestCase {
164164
XCTAssertEqual(.int(42), Foo.bar(.int(42))[case: partialPath] as? Bar)
165165
XCTAssertNil(Foo.baz(.string("Hello"))[case: partialPath])
166166
}
167+
168+
func testExistentials() {
169+
let caseA: PartialCaseKeyPath<A> = \.a
170+
let caseB: PartialCaseKeyPath<B> = \.b
171+
172+
let a = A.a("Hello")
173+
guard let valueA = a[case: caseA] else { return XCTFail() }
174+
guard let b = caseB(valueA) else { return XCTFail() }
175+
XCTAssertEqual(b, .b("Hello"))
176+
}
167177
#endif
168178
}
169179

170180
#if swift(>=5.9)
181+
@CasePathable
182+
enum A: Equatable {
183+
case a(String)
184+
}
185+
186+
@CasePathable
187+
enum B: Equatable {
188+
case b(String)
189+
}
190+
171191
@CasePathable @dynamicMemberLookup enum Foo: Equatable {
172192
case bar(Bar)
173193
case baz(Baz)

0 commit comments

Comments
 (0)