Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Swift Dictionary

  1. Home
  2. Swift Dictionary
  3. some / any / opaque type

some / any / opaque type

Since Swift 5.7, two keywords — some (opaque type) and any (existential type) — have been introduced as ways to use protocols as types. Both allow you to work with a protocol as a type, but in different ways.

Syntax

// some: opaque type (return value is fixed to one specific concrete type)
func makeShape() -> some Shape {
    return Circle(radius: 5)
}

// any: existential type (can store any type conforming to the protocol)
var shapes: [any Shape] = [Circle(radius: 5), Rectangle(width: 3, height: 4)]

// Primary associated type (Swift 5.7+)
func process(_ values: some Collection<Int>) { }

Syntax Overview

SyntaxDescription
some ProtocolOpaque type. The compiler knows the concrete type, but it is hidden from the caller. Used for return types and parameter types.
any ProtocolExistential type. Can store any type that conforms to the protocol. Used when mixing different types in an array.
some Collection<T>Primary associated type syntax. Specifies that the elements of the Collection are of type T.

Sample Code

// Protocol definition
protocol Animal {
    var name: String { get }
    func sound() -> String
}

struct Dog: Animal {
    let name: String
    func sound() -> String { return "Woof" }
}

struct Cat: Animal {
    let name: String
    func sound() -> String { return "Meow" }
}

// some: the return value is a single fixed concrete type (hidden from the caller)
func makeDog() -> some Animal {
    return Dog(name: "Buddy")
}

let dog = makeDog()
print("\(dog.name): \(dog.sound())")

// any: existential type that can hold different concrete types under the same type
let animals: [any Animal] = [
    Dog(name: "Buddy"),
    Cat(name: "Whiskers"),
    Dog(name: "Max")
]

for animal in animals {
    print("\(animal.name): \(animal.sound())")
}

// Difference between some and any
// some: the function always returns the same concrete type (can be optimized by the compiler)
// any: can store any type (incurs overhead due to boxing)

// Primary associated type syntax (Swift 5.7+)
func sumAll(_ numbers: some Collection<Int>) -> Int {
    return numbers.reduce(0, +)
}

print("Total: \(sumAll([1, 2, 3, 4, 5]))")
print("Total: \(sumAll(Set([10, 20, 30])))")

Notes

some is an opaque type that "hides the type from the caller, while the compiler knows the concrete type." Use it when a function always returns the same concrete type — it offers good performance. A typical example is body: some View in SwiftUI.

any is an existential type that "can store any type conforming to the protocol." Use it when the type may vary at runtime, such as when mixing different types in an array. Since Swift 5.7, the any keyword is required when using a protocol as a type. Be careful not to confuse it with some.

For generic functions, see Generic Functions / Type Parameters.

If you find any errors or copyright issues, please .