Logo for tanaschita.com

Understanding opaque types and protocols with associatedtype in Swift

Learn how to leverage the some keyword for working with protocols with associated types.

06 Jan 2025 · 2 min read

Protocols with associatedtype or Self requirements often bring challenges when used as parameters or return types in Swift. Historically, developers needed to rely on generics to work around these limitations, leading to sometimes complex code.

This article will show how to use opaque types to simplify interactions with such protocols.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

As an example, let's look at a protocol with an associated type.

protocol Store {
associatedtype Item
func persist(item: Item)
}

Because of the associatedtype, the protocol does not have a fully determined concrete type until we provide a specific implementation. This makes it impossible to use the protocol directly as a parameter or return type without triggering a compile-time error.

For example, writing the following function results in an error:

func cleanup(store: Store) {
// Error: Protocol 'Store' can only be used as a generic constraint because it has Self or associated type requirements.
}

To work around this, we could define the function as a generic:

func cleanup<T: Store>(store: T) {
}

While this works, it introduces complexity. By using opaque types, we can simplify:

func cleanup(store: some Store) {
// Simplified function declaration.
}

The opaque declaration is basically syntactic sugar for the equivalent generic code. The compiler will automatically infer the concrete parameter type without needing us to write the generic code.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

generics and protocols

swift

Understanding existentials and primary associated types in Swift

How existentials and primary associated types enhance flexibility in protocols.

17 Mar 2025 · 5 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum