---
title: "Using protocol members with references to `Self` or `Self`-rooted associated types (ExistentialMemberAccess)"
framework: swift-compiler
role: article
role_heading: Article
path: swift-compiler/documentation/diagnostics/existential-member-access-limitations
---

# Using protocol members with references to `Self` or `Self`-rooted associated types (ExistentialMemberAccess)

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:

## Overview

Overview // An appropriately constrained generic parameter. func foo<T: CustomStringConvertible>(arg: T) {   let description: String = arg.description }

do {   // An appropriately constrained opaque result type.   func foo() -> some CustomStringConvertible { true }

let description: String = foo().description }

// The protocol type. func foo(arg: CustomStringConvertible) {   let description: String = arg.description } 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: protocol Shape {   func matches(_ other: Self) -> Bool }

func foo(_ shape: Shape) {   // error: member 'matches' cannot be used on value of protocol type 'Shape'; use a generic constraint instead   shape.matches(shape) }

func foo(_ arg: Identifiable) {   // error: member 'id' cannot be used on value of protocol type 'Identifiable'; use a generic constraint instead   _ = arg.id } An exception to this limitation are members that contain Self only in covariant 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. protocol Shape {   func duplicate() -> Self }

func duplicateShape(_ shape: Shape) -> Shape {   return shape.duplicate // OK, produces a value of type 'Shape' } 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, Generics, and Opaque Types 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 Improving the UI of generics.

## See Also

### Related Documentation

- [Generics](swift-compiler/documentation/swift-book/documentation/the-swift-programming-language/generics.md)
- Improving the UI of generics
- [Opaque Types](swift-compiler/documentation/swift-book/documentation/the-swift-programming-language/opaquetypes.md)
- [Protocols](swift-compiler/documentation/swift-book/documentation/the-swift-programming-language/protocols.md)

- [@dynamicCallable implementation requirements (DynamicCallable)](swift-compiler/documentation/diagnostics/dynamic-callable-requirements.md)
- [Add @preconcurrency import (AddPreconcurrencyImport)](swift-compiler/documentation/diagnostics/add-preconcurrency-import.md)
- [Always enabled availability domains (AlwaysAvailableDomain)](swift-compiler/documentation/diagnostics/always-available-domain.md)
- [Argument matching for trailing closures (TrailingClosureMatching)](swift-compiler/documentation/diagnostics/trailing-closure-matching.md)
- [Calling a mutating async actor-isolated method (ActorIsolatedMutatingAsync)](swift-compiler/documentation/diagnostics/actor-isolated-mutating-async.md)
- [Calling an actor-isolated method from a synchronous nonisolated context (ActorIsolatedCall)](swift-compiler/documentation/diagnostics/actor-isolated-call.md)
- [Captures in a `@Sendable` closure (SendableClosureCaptures)](swift-compiler/documentation/diagnostics/sendable-closure-captures.md)
- [Compilation caching (CompilationCaching)](swift-compiler/documentation/diagnostics/compilation-caching.md)
- [Conforming to `StringInterpolationProtocol` (StringInterpolationConformance)](swift-compiler/documentation/diagnostics/string-interpolation-conformance.md)
- [Conversion from `@isolated(any)` function type to synchronous function type (ConversionFromIsolatedAnyToSynchronous)](swift-compiler/documentation/diagnostics/conversion-from-isolated-any-to-synchronous.md)
- [Cross-isolation data race (RegionIsolationCrossIsolationDataRace)](swift-compiler/documentation/diagnostics/region-isolation-cross-isolation-data-race.md)
- [Deprecated declaration warnings (DeprecatedDeclaration)](swift-compiler/documentation/diagnostics/deprecated-declaration.md)
- [Deprecated implementation-only imports (ImplementationOnlyDeprecated)](swift-compiler/documentation/diagnostics/implementation-only-deprecated.md)
- [Dynamic exclusivity (DynamicExclusivity)](swift-compiler/documentation/diagnostics/dynamic-exclusivity.md)
- [Embedded Swift language restrictions (EmbeddedRestrictions)](swift-compiler/documentation/diagnostics/embedded-restrictions.md)
