Given the following code snippet from an unmodifiable source
protocol P {}
protocol D {
func foo<K: P>(_: K.Type)
}
extension Int: D {
func foo<K: P>(_: K.Type) { print("Int P") } // <0>
}
I wonder if there is a way to provide a more specific overload of foo(_:)
by means of extending the protocol D
. In detail, I'd like to add a new protocol Q
and an overload for when K: P & Q
(or, more generally, a new constraint on K
):
protocol Q {}
struct S: P & Q {}
extension D {
func foo<K: P & Q>(_: K.Type) { print("P & Q") } // <1>
}
0.foo(S.self) // prints "Int P", not "P & Q"
I can get the desired behavior only if I use S
directly in the function signature:
extension D {
func foo(_: S.Type) { print("S") } // <2>
}
0.foo(S.self) // prints "S"
Is this intended behavior? Intuitively, I would assume that either both <1>
and <2>
take precedence over <0>
or none does.