mapping on SharedReader #3137
-
I have the following case with nested wrappers. I want to drop the second wrapper ( Mapping in the enum Wrapper<S> {
case value(S)
case none
}
enum Wrapper2<S> {
case value(S)
case none
}
struct Foo {
@SharedReader var count: Wrapper<Int>
init(_ count: SharedReader<Wrapper<Wrapper2<Int>>>) {
self._count = // TODO
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @OguzYuuksel, this can be solved with dynamic member look up. You want to transform a enum Wrapper<S> {
case value(S)
case none
}
enum Wrapper2<S> {
case value(S)
case none
}
extension Wrapper {
subscript<Value>() -> Wrapper<Value> where S == Wrapper2<Value> {
get {
switch self {
case .value(.value(let value)):
.value(value)
case .value(.none), .none:
.none
}
}
set {
switch newValue {
case .value(let value):
self = .value(.value(value))
case .none:
self = .none
}
}
}
}
import SwiftUI
func foo() {
@State var x = Wrapper<Wrapper2<Int>>.none
$x[] as Binding<Wrapper<Int>>
} It's strange, of course, but I assume this is just temporary code as you try to get rid of one of the wrappers? |
Beta Was this translation helpful? Give feedback.
Hi @OguzYuuksel, this can be solved with dynamic member look up. You want to transform a
Wrapper<Wrapper2<…>>
into aWrapper<…>
, which you can do via asubscript
onWrapper
: