Self (capital S) can be used within protocols and protocol extensions (to mean the conforming type).
But within structs, we must use the actual name of the type, as demonstrated by this contrived example:
protocol Pairable {
func pairedWith(other: Self) -> (Self, Self)
}
extension Pairable {
func pairedWith(other: Self) -> (Self, Self) { return (self, other) }
}
struct UsingDefaultImpl : Pairable {}
struct ReversedPairable {
func pairedWith(other: ReversedPairable) -> (ReversedPairable, ReversedPairable) { return (other, self) }
// Ok, but why not simply:
// func pairedWith(other: Self) -> (Self, Self) { return (other, self) }
// ?
}
extension ReversedPairable : Pairable {}So, what's the rationale behind not allowing Self within a struct?
(It can't be to to avoid mixing up Self and self. (See the default implementation for pairedWith in the protocol extension above.))