I am surprised by this behavior (I barely use reference types, but when I do, I never referenced Self
).
I found a post explaining it (with a lot of discussion): Why Is Covariant Self more flexible on protocols than classes? - #3 by Slava_Pestov . Basically it behaves differently due to interoperability with Objective-C.
Without that understanding this would be very confusing:
func g() -> Self {
C() // Error: Cannot convert return expression of type 'C' to return type 'Self'
}
func h() -> C {
Self() // Ok
}
Self
is a headache in reference types. Duly noted.