diff --git a/Sources/CasePathsCore/CasePathable.swift b/Sources/CasePathsCore/CasePathable.swift index a4565118..a890f3f3 100644 --- a/Sources/CasePathsCore/CasePathable.swift +++ b/Sources/CasePathsCore/CasePathable.swift @@ -493,6 +493,13 @@ extension CasePathable { } } +extension CasePathable where AllCasePaths: CasePathReflectable { + /// A case key path to this enum's case. + public var `case`: PartialCaseKeyPath { + Self.allCasePaths[self] + } +} + extension AnyCasePath { /// Creates a type-erased case path for given case key path. /// diff --git a/Sources/CasePathsCore/Documentation.docc/Extensions/CasePathable.md b/Sources/CasePathsCore/Documentation.docc/Extensions/CasePathable.md index cecb804e..bbe779f3 100644 --- a/Sources/CasePathsCore/Documentation.docc/Extensions/CasePathable.md +++ b/Sources/CasePathsCore/Documentation.docc/Extensions/CasePathable.md @@ -14,6 +14,7 @@ ### Case properties +- ``case`` - ``is(_:)`` - ``subscript(dynamicMember:)-emck`` - ``subscript(dynamicMember:)-dm5y`` diff --git a/Sources/CasePathsCore/Optional+CasePathable.swift b/Sources/CasePathsCore/Optional+CasePathable.swift index 9b9ff213..6753e7cc 100644 --- a/Sources/CasePathsCore/Optional+CasePathable.swift +++ b/Sources/CasePathsCore/Optional+CasePathable.swift @@ -32,6 +32,7 @@ extension Optional: CasePathable, CasePathIterable { /// A case path to an optional-chained value. @_disfavoredOverload + @available(*, deprecated, message: "This subscript will be removed in CasePaths 2.0") public subscript( dynamicMember keyPath: KeyPath> ) -> AnyCasePath diff --git a/Tests/CasePathsTests/CasePathsTests.swift b/Tests/CasePathsTests/CasePathsTests.swift index 4e1a7405..9f1fb715 100644 --- a/Tests/CasePathsTests/CasePathsTests.swift +++ b/Tests/CasePathsTests/CasePathsTests.swift @@ -60,6 +60,8 @@ final class CasePathsTests: XCTestCase { func testCaseKeyPaths() { var foo: Foo = .bar(.int(1)) + XCTAssertEqual(foo.case, \.bar) + XCTAssertEqual(foo.bar, .int(1)) // NB: Due to a Swift bug, this is only possible to do outside the library: // XCTAssertEqual(foo.bar?.int, 1)