My question is at the end of this program:
//-------------------------------------------------------------------------
// Simple construct of two protocols P and Q:
//-------------------------------------------------------------------------
protocol P {
var id: String {get}
}
extension P {
var id: String { "P" }
var id2: String { id }
}
protocol Q: P {}
extension Q {
var id: String { "Q" }
}
//-------------------------------------------------------------------------
// Concrete types X Y Z, each conforming to P and Q, but in different ways:
//-------------------------------------------------------------------------
struct X<T> {}
extension X: P {}
extension X: Q {}
struct Y<T> {}
extension Y: P {}
extension Y: Q where T: Equatable {}
struct Z<T> {}
extension Z: P where T: Equatable {}
extension Z: Q where T: Equatable {}
//-------------------------------------------------------------------------
// Instances x: X, y: Y and z: Z. Their .id and .id2 are printed, first in
// the same context as their declaration, then in generic context <T: Q>.
//-------------------------------------------------------------------------
func printGenericConstrainedToQ<T: Q>(_ a: T) { print(a.id, a.id2) }
let (x, y, z) = (X<Int>(), Y<Int>(), Z<Int>())
print(x.id, x.id2) // Q Q (1)
print(y.id, y.id2) // Q P (2)
print(z.id, z.id2) // Q Q (3)
printGenericConstrainedToQ(x) // Q Q (4)
printGenericConstrainedToQ(y) // P P (5)
printGenericConstrainedToQ(z) // Q Q (6)
//------------------------------------------------------------------------
// Question: Are all of these six printed results as intended?
// - If so, can anyone help me wrap my head around (2) and (5)?
// - If not, what should they be?
//------------------------------------------------------------------------
(This program is based on an example by @dabrahams which is based on an example by @mattrips in this thread).