Opaque Types with some Keyword
Returning opaque types from functions to preserve type identity without exposure.
Opaque Types with some Keyword is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
The Problem Opaque Types Solve
Returning a protocol type with any loses type information. some preserves it while hiding implementation.
protocol Shape { var area: Double { get } }
struct Circle: Shape { var area: Double { .pi * 4 } }Returning some Protocol
some Shape as a return type tells the caller "I return a specific Shape type" without revealing which one.
func makeShape() -> some Shape {
return Circle()
}
let s = makeShape()
print(s.area)Type Identity is Preserved
Because some types have a fixed underlying type, the compiler allows operations requiring Self.
func makeEquatableShape() -> some Shape & Equatable {
return Circle()
}
let a = makeEquatableShape()
let b = makeEquatableShape()
print(a == b) // works because types matchsome vs any
some is opaque (single fixed type), any is existential (any conforming type at runtime).
func withSome() -> some Shape { Circle() } // opaque
func withAny() -> any Shape { Circle() } // existentialsome in SwiftUI Body
SwiftUI uses some View so that each view's body returns a single consistent type without exposing layout internals.
struct ContentView: View {
var body: some View {
Text("Hello, World!")
}
}Opaque Parameters with some
Swift 5.7+ allows some Protocol in parameter positions as syntactic sugar for generic constraints.
func printArea(_ shape: some Shape) {
print(shape.area)
}
printArea(Circle())Reversed Generics
An opaque parameter some P is equivalent to . The compiler infers T at the call site.
func doubled(_ value: some Numeric) -> some Numeric {
value * 2
}Constraints on some
You can compose protocols with opaque types just like generics.
func process(_ item: some Codable & Hashable) {
let hash = item.hashValue
print(hash)
}Returning Different Types
Unlike existentials, you cannot return different conforming types from different branches with opaque types.
// This would NOT compile:
// func shape(_ big: Bool) -> some Shape {
// big ? Circle() : Square() // ERROR: different types
// }Opaque Types in Protocols
Protocols with associated types become simpler to use as return types with some.
protocol Buildable { associatedtype Output; func build() -> Output }
func build(_ b: some Buildable) -> some Any {
b.build()
}When to Choose some
Prefer some when you return one consistent type and want to preserve identity. Use any when you need heterogeneous collections.
var shapes: [any Shape] = [Circle(), Circle()]
let single: some Shape = Circle()Quick Check
What does the some keyword guarantee about a return type?
Lesson Recap
some creates opaque return types that hide the concrete type while preserving type identity. SwiftUI's some View is the canonical example. Use some for single-type returns, any for heterogeneous collections.
Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Opaque Types with some Keyword” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Opaque Types with some Keyword”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “Opaque Types with some Keyword”?
Returning opaque types from functions to preserve type identity without exposure. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Opaque Types with some Keyword” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Generic Constraints and where Clauses
- Opaque Types with some Keyword
- Existentials with any and Type Erasure
- Primary Associated Types and Typed Throws