|
| 1 | +# Using Protocol Members with References to `Self` or `Self`-rooted Associated Types |
| 2 | + |
| 3 | +Protocol requirements and protocol extension members may be accessed via a conformance constraint on a generic parameter, an opaque result type, or via the protocol type itself: |
| 4 | + |
| 5 | +```swift |
| 6 | +// An appropriately constrained generic parameter. |
| 7 | +func foo<T: CustomStringConvertible>(arg: T) { |
| 8 | + let description: String = arg.description |
| 9 | +} |
| 10 | + |
| 11 | +do { |
| 12 | + // An appropriately constrained opaque result type. |
| 13 | + func foo() -> some CustomStringConvertible { true } |
| 14 | + |
| 15 | + let description: String = foo().description |
| 16 | +} |
| 17 | + |
| 18 | +// The protocol type. |
| 19 | +func foo(arg: CustomStringConvertible) { |
| 20 | + let description: String = arg.description |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +While the former two options enable full access to the protocol interface, not all members may be accessible when the protocol is used as a type and not a constraint. Specifically, a protocol member cannot be accessed on a protocol type when its type signature contains a reference to `Self` or a `Self`-rooted associated type. Accessing such members on a protocol type is not supported because today the compiler does not have a well-defined meaning and means of representation for `Self` and `Self`-rooted associated types with respect to a protocol type `P`. As a result, the following code is not allowed: |
| 25 | + |
| 26 | +```swift |
| 27 | +protocol Shape { |
| 28 | + func matches(_ other: Self) -> Bool |
| 29 | +} |
| 30 | + |
| 31 | +func foo(_ shape: Shape) { |
| 32 | + // error: member 'matches' cannot be used on value of protocol type 'Shape'; use a generic constraint instead |
| 33 | + shape.matches(shape) |
| 34 | +} |
| 35 | + |
| 36 | +func foo(_ arg: Identifiable) { |
| 37 | + // error: member 'id' cannot be used on value of protocol type 'Identifiable'; use a generic constraint instead |
| 38 | + _ = arg.id |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +An exception to this limitation are members that contain `Self` only in [covariant](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)) position (such as a method result type), where `Self` can be safely substituted with the protocol or protocol composition type used to access the member — a representable supertype. On the other hand, resorting to this ploy in contravariant parameter type position, like allowing one to pass a type-erased value to a method that accepts `Self`, is not type-safe and would expose the opportunity to pass in an argument of non-matching type. |
| 43 | + |
| 44 | +```swift |
| 45 | +protocol Shape { |
| 46 | + func duplicate() -> Self |
| 47 | +} |
| 48 | + |
| 49 | +func duplicateShape(_ shape: Shape) -> Shape { |
| 50 | + return shape.duplicate // OK, produces a value of type 'Shape' |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Most use cases involving usage of protocol members that fall under the above restriction can instead be supported by constrained generics, opaque result types, or manual type-erasing wrappers. To learn more, see the sections on [protocols](https://docs.swift.org/swift-book/LanguageGuide/Protocols.html), [generics](https://docs.swift.org/swift-book/LanguageGuide/Generics.html), and [opaque types](https://docs.swift.org/swift-book/LanguageGuide/OpaqueTypes.html) in the Language Guide. For a better understanding of existential types in particular, and an in-depth exploration of the relationships among these built-in abstraction models, we recommend reading the [design document for improving the UI of the generics model](https://forums.swift.org/t/improving-the-ui-of-generics/22814). |
0 commit comments