To provide some context:
P stands for property. The purpose of the code is that values of different types should be handled by individual methods (e.g. serializeInt, serializeDouble etc.), something like method overloading but the type of the argument coming from a type parameter.
The code below actually works fine. It calls the specialized pr( _: Int) implementation and prints "int".
But if I change the declaration "func pr(_ t: Int)" to the commented out one "func pr(_ t: T)", then the generic version gets called.
Anyone has any pointers to where this behavior is specified or why it works like this?
protocol P {
associatedtype T
// this will be 'specialized' for concrete types
func pr(_ t: T)
// the operation that should call different pr implementations depending on T
func op(_ t: T)
}
extension P {
func op(_ t: T) {
pr(t)
}
}
// fallback implementation
extension P {
func pr(_ t: T) {
print("generic")
}
}
// pr 'specialized' on Int
extension P where T == Int {
// func pr(_ t: T) {
func pr(_ t: Int) {
print("int")
}
}
struct Prop<T>: P {
}
// create an Int prop and do the op
let p = Prop<Int>()
p.op(1)