Skip to content

Commit 45d8ed7

Browse files
authored
Simplify optional flattening (#140)
* Simplify optional flattening Turns out we only need the `Case` overload. * Update Sources/CasePaths/Optional+CasePathable.swift
1 parent a15f99d commit 45d8ed7

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

Sources/CasePaths/Never+CasePathable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ extension Never: CasePathable {
66
}
77

88
extension Case {
9+
/// A case path that can never embed or extract a value.
10+
///
11+
/// This property can chain any case path into a `Never` value, which, as an uninhabited type,
12+
/// cannot be embedded nor extracted from an enum.
913
public var never: Case<Never> {
1014
func absurd<T>(_: Never) -> T {}
1115
return Case<Never>(embed: absurd, extract: { (_: Value) in nil })

Sources/CasePaths/Optional+CasePathable.swift

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extension Optional: CasePathable {
2-
@dynamicMemberLookup
32
public struct AllCasePaths {
3+
/// A case path to the absence of a value.
44
public var none: AnyCasePath<Optional, Void> {
55
AnyCasePath(
66
embed: { .none },
@@ -11,6 +11,7 @@ extension Optional: CasePathable {
1111
)
1212
}
1313

14+
/// A case path to the presence of a value.
1415
public var some: AnyCasePath<Optional, Wrapped> {
1516
AnyCasePath(
1617
embed: { .some($0) },
@@ -20,20 +21,6 @@ extension Optional: CasePathable {
2021
}
2122
)
2223
}
23-
24-
public subscript<Member>(
25-
dynamicMember keyPath: KeyPath<Wrapped.AllCasePaths, AnyCasePath<Wrapped, Member>>
26-
) -> AnyCasePath<Optional, Member>
27-
where Wrapped: CasePathable {
28-
let casePath = Wrapped.allCasePaths[keyPath: keyPath]
29-
return AnyCasePath(
30-
embed: { .some(casePath.embed($0)) },
31-
extract: {
32-
guard case let .some(value) = $0 else { return nil }
33-
return casePath.extract(from: value)
34-
}
35-
)
36-
}
3724
}
3825

3926
public static var allCasePaths: AllCasePaths {
@@ -42,6 +29,10 @@ extension Optional: CasePathable {
4229
}
4330

4431
extension Case {
32+
/// A case path to the presence of a nested value.
33+
///
34+
/// This subscript can chain into an optional's wrapped value without explicitly specifying each
35+
/// `some` component.
4536
public subscript<Member>(
4637
dynamicMember keyPath: KeyPath<Value.AllCasePaths, AnyCasePath<Value, Member?>>
4738
) -> Case<Member>

Sources/CasePaths/Result+CasePathable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extension Result: CasePathable {
22
public struct AllCasePaths {
3+
/// A success case path, for embedding or extracting a `Success` value.
34
public var success: AnyCasePath<Result, Success> {
45
AnyCasePath(
56
embed: { .success($0) },
@@ -10,6 +11,7 @@ extension Result: CasePathable {
1011
)
1112
}
1213

14+
/// A failure case path, for embedding or extracting a `Failure` value.
1315
public var failure: AnyCasePath<Result, Failure> {
1416
AnyCasePath(
1517
embed: { .failure($0) },

0 commit comments

Comments
 (0)