0Pricing
Swift Academy · Lesson

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 — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 match

some 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() }   // existential

some 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 (_ value: T). 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.

Frequently asked questions

Is the “Opaque Types with some Keyword” lesson free?

Yes — the full text of “Opaque Types with some Keyword” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Generic Constraints and where Clauses
  2. Opaque Types with some Keyword
  3. Existentials with any and Type Erasure
  4. Primary Associated Types and Typed Throws
← Back to Swift Academy