Okay, this is a better example showing where my mental blockage:
import Foundation
import SwiftUI
protocol Something { }
extension Double: Something { }
func convert<T: Something>(_ binding: Binding<T>) {
print("convert, T is \(T.self)")
// ??? How does it know T is what?
if let value = NSNumber(value: 123) as? T { // my example, how does it get Double here?
binding.wrappedValue = value
} else {
fatalError("cannot convert type: \(T.self)")
}
}
var abstractD: Something = 123.4
let abstractDBinding = Binding(get: { abstractD }, set: { abstractD = $0 })
convert(abstractDBinding) // compile error: Protocol 'Something' as a type cannot conform to the protocol itself
print("abstractD = ", abstractD)
It's just strange to me it's this way, even though I've read protocol X does not conform to X.
I keep think convert<T: Something>, then T is only known to the compiler as Something. But it's really just "conformance" requirement. The real type is still used.