Add a protocol for subscripting? #93
finestructure
started this conversation in
Ideas
Replies: 1 comment 2 replies
-
Hey Sven! Without weighting on the protocol CasePathed {
subscript<Value>(casePath casePath: CasePath<Self, Value>) -> Value? { get set }
}
extension CasePathed {
subscript<Value>(casePath casePath: CasePath<Self, Value>) -> Value? {
get {
return casePath.extract(from: self)
}
set {
guard let newValue else { return }
self = casePath.embed(newValue)
}
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
There's a bit of an asymmetry when it comes to using CasePaths vs structs that's been bothering me to the point where I can never quite remember the syntax 😅.
Here's what I mean (using type definitions from the README). We read and write via keypath subscripts as follows:
yet the same for CasePaths looks like this:
It would make more sense to me to be able to do the following:
I've added this via a protocol
CasePathed
(or maybeCasePathSubscripted
,CasePathSubscriptable
?) that can be adopted by enums:and then simply
I'm sure you've considered this. Is this a bad idea? Not worth it due to having to mark up your enums?
Beta Was this translation helpful? Give feedback.
All reactions