Two overloaded Binding.init<V>(Binding<V>) how to get the closure of each?

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?

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
Terms of Service

Privacy Policy

Cookie Policy