Types cannot be nested in protocols:
protocol P {
struct S {} // 🛑 Type 'S' cannot be nested in protocol 'P'
}
But typealiases can:
protocol P {
typealias X = Int // OK
}
I can see how this can be useful but I'm confused by for example the following.
This compiles:
protocol P {
typealias X = Int
}
extension P {
typealias X = Bool
}
struct A: P {}
Though print(S.X.self)
prints Int
. I would have expected Bool
.
But, since the following doesn't compile, I guess the previous example shouldn't compile either, right?
protocol P { // 🛑 No type for 'Self.X' can satisfy both 'Self.X == Bool' and 'Self.X == Int'
typealias X = Int
// }
// extension P {
typealias X = Bool // 🛑 Invalid redeclaration of 'X'
}
struct A: P {}
Another example:
protocol P {
typealias X = Int
}
extension P where Self == B {
typealias X = Bool
}
extension P where Self == C {
typealias X = String
}
struct A: P {}
struct B: P {}
struct C: P {}
print(A.X.self)
print(B.X.self)
print(C.X.self)
I would expect this to print:
Int
Bool
String
But all three prints Int
...
What is the intended behavior here?
(I'm using Swift 5.7, not aware of any changes related to this.)