Brifing

Unlocking Swift's Opaque Return Types Beyond SwiftUI Ecosystem

How Swift's some keyword streamlines generics, eliminates type erasure, and improves API design.

Swift by Sundell2 min

1Opaque Types Beyond SwiftUI Views

Swift developers primarily know the some keyword through SwiftUI view declarations to hide complex nested types. However, opaque return types serve broader architectural purposes. They simplify complex generic codebases across entire Swift applications without incurring runtime overhead or forcing developers to expose private implementation details.

2Simplifying Generic Function Parameters

Accepting collections previously required defining explicit generic type parameters with where clauses, such as Sequence constraints. Swift allows developers to replace verbose syntax with primary associated types. Writing some Sequence<Video> directly in function signatures clarifies intent, preserves type safety, and removes unnecessary boilerplate.

3Eliminating Public Implementation Leakage

Returning concrete types from networking services or packages exposes internal models to consuming call sites. If a framework exposes distinct operation structs publicly, it bloats public interfaces. Maintaining clean boundaries requires hiding underlying execution details while still returning a strongly typed, predictable protocol-conforming instance.

4The Downside of Manual Erasure

Historically, hiding concrete types required manual type erasure using wrapper structs like AnyOperation. This approach captures closures, introduces boilerplate wrappers, and complicates architectural maintenance. While type erasure decouples call sites from underlying implementations, it introduces unnecessary manual coding overhead and structural complexity.

5Streamlining APIs With Opaque Protocols

By returning some Operation<Data>, methods can keep concrete structs private while exposing clean contracts. The compiler resolves the concrete type automatically, eliminating custom wrapper structs. This clean separation of concerns maintains modular boundaries across asynchronous operations without writing tedious manual type-erasure layers.

6Compile Time Versus Runtime Resolution

A critical advantage of the some keyword over the any keyword is resolution timing. Opaque types are resolved entirely at compile time, guaranteeing zero runtime performance cost. Developers retain full generic type constraints without boxing values into dynamically resolved existential containers.

7Differentiating Opaque and Existential Types

While any provides existential containers resolved dynamically at runtime, some preserves static type identities. Existential boxing introduces memory allocations and dynamic dispatch overhead when accessing underlying members. Choosing opaque types allows Swift compilers to optimize calls directly, ensuring peak runtime performance across complex systems.

8Architectural Takeaways for Swift Developers

Swift's some keyword represents a foundational tool for expressive API design rather than a SwiftUI novelty. It elegantly solves generic clutter, replaces cumbersome type erasers, and hides private implementation details. Adopting opaque types creates cleaner, maintainable Swift architectures without compromising application speed.

Original article: Swift by Sundell — Unlocking Swift's Opaque Return Types Beyond SwiftUI Ecosystem