young
(rtSwift)
August 23, 2021, 4:00pm
1
SwiftUI.Binding has these two overloaded init
's:
A: init<V>(_ base: Binding<V>) where Value == V?
B: init<V>(_ base: Binding<V>) where Value == AnyHashable, V : Hashable
The signature is the same:
init<V>(Binding<V>) // init(_:)
What's the syntax to get the closure of A and B?
let closureToA = Binding<let me have A how??>.init(_:) // how to specify generic parameter type for A?
let closureToB = Binding<let me have B how??>.init(_:) // and for B?
Jumhyn
(Frederick Kellison-Linn)
August 23, 2021, 4:09pm
2
Currently, it's not possible to have generic closures, so closureToA
and closureToB
will have to have specific types. You could, for example, do this:
let closureToA: (Binding<Int>) -> Binding<Int?> = Binding.init(_:)
let closureToB: (Binding<Int>) -> Binding<AnyHashable> = Binding.init(_:)
You could also declare functions that mimic the generic signatures of the initializers in question:
func funcToA<T>(_ base: Binding<T>) -> Binding<T?> {
return .init(base)
}
func funcToB<T>(_ base: Binding<T>) -> Binding<AnyHashable> where T: Hashable {
return .init(base)
}
2 Likes