Understanding some and any in Swift

Saeid Rezaeisadrabadi
3 min readMay 23, 2023

In this story, I’m going to talk about two magic keywords in Swift, some and any. These are not new, but they are valuable. These keywords play a significant role in type erasure and allow for more dynamic and abstract programming.

Understanding “some"

The some keyword in Swift is used to define an opaque type. An opaque type allows you to hide the underlying concrete type and only expose the capabilities defined by a protocol. This is particularly useful when you want to provide an abstract interface to clients without revealing implementation details.

Let’s consider an example. Suppose we have a protocol called Shape that defines the basic functionality of a shape:

protocol Shape {
func draw()
}

I can create various concrete shapes that conform to this protocol, such as Circle, Rectangle, and Triangle. Now, let's define a function called drawRandomShape that returns a random shape:

func drawRandomShape() -> some Shape {
let randomIndex = Int.random(in: 0...2)

switch randomIndex {
case 0:
return Circle()
case 1:
return Rectangle()
case 2:
return Triangle()
default:
fatalError("Invalid random index")
}
}

I use some Shape as the return type. This means the function returns an opaque type that conforms to the Shape protocol. The actual concrete type is hidden from the client, allowing for a more abstract and generic interface.

What about “any”?

While some allows us to hide the underlying type and provide an opaque interface, there are cases where we need to work with multiple types that conform to a common protocol. This is where the any keyword comes into play.

In Swift, any is used to erase the type information of a value or expression and treat it as a more general type. This is called type erasure. It allows you to work with a collection of different types that share a common protocol, without needing to specify the exact types.

Let’s consider an example where we have a protocol called SuperPower:

protocol SuperPower {
func usePower()
}

Now, suppose we have different heroes, such as SuperMan, Thor, and IronMan, that conforms to this protocol. We want to create a collection of heroes and execute the usePower() method on each of them. We can use the any keyword to achieve this:

let heroes: [any SuperPower] = [
SuperMan(),
Thor(),
IronMan()
]

for hero in heroes {
hero.usePower()
}

the heroes array contains instances of different types, all conforming to the SuperPower protocol. By using any SuperPower, we can store these instances in a homogeneous collection and invoke the usePower() method on each of them without knowing their concrete types.

Conclusion

Understanding some and any in Swift is crucial for building flexible and generic code. The some keyword allows you to define opaque types, providing an abstract interface to clients. It enables you to hide implementation details and work with protocols more generically. On the other hand, the any keyword is used for type erasure, allowing you to work with collections of different types that conform to a common protocol without exposing the concrete types.

By leveraging these language features, you can design more modular and extensible code, making your applications easier to maintain and scale.

It’s important to note that the use of some and any should be considered thoughtfully. While they offer flexibility and abstraction, excessive use can lead to loss of type safety and decreased clarity in code. It's essential to strike a balance between abstracting away implementation details and maintaining the necessary type of information for correctness and readability.

In conclusion, some and any are powerful tools in Swift that enable type erasure and abstraction. They allow you to hide concrete types, work with protocols generically, and create more flexible and modular code. By understanding these concepts and using them judiciously, you can enhance your programming skills and create more robust and adaptable applications with Swift.

--

--

Saeid Rezaeisadrabadi
Saeid Rezaeisadrabadi

Written by Saeid Rezaeisadrabadi

Over 8 years of experience in iOS software development

No responses yet