Some any new generic features

By Denis Poifol

Elevator Pitch

Protocols in swift get a huge update with Swift 5.7 🤯 I will go with you through the use case of the some and any keywords to help understand the concept they each represent. We will talk about existential types, type constraint that are capital to truly understand protocol in Swift 5.7 🤓

Description

Generics and protocols are the most flexible tool at our disposition to build complex algorithms and models. But failing to understand how and when to use them might have disastrous consequences for our codebase. The most frequent misunderstanding of protocols in swift comes from the duality of their implementation in the language.

The Swift 5.7 update tries to disambiguate two very different side of protocols and helps us learn a bit more about how to use them.

The two sides of protocols are type constraint and existential types Until this new version of swift the two were expressed the same way which brought a lot of confusion for many Swift developers. The most common misunderstanding of these concept often lead to the famous : Protocol can only be used as a generic constraint because it has Self or associatedType requirements 🤬.

The goal here will be to understand what is an existential type and what is a type constraint. From there we will be able to understand what did this error mean and how with Swift 5.7 we probably won’t have this issue anymore thanks to parametrized protocols and a generalization of the existential types. But as we go deeper into existential types for protocol with associated types we will see the limitations we must face on those. For the experts we might even talk about co/contra-variance to explain these limitations. 🧐

There is a lot to unpack regarding protocols/generics in Swift 5.7 And as some like myself might be eager to dig in, other might feel a bit overwhelmed with all those “new” concepts. This talk aims at bringing information for everyone in the audience, wether they are discovering Swift protocols or trying to dig deeper into the subject.

Notes

Generics and protocols are my favorite subject because they enable to do seamlessly the most complicated task in a code base. Arrays and collections in general in Swift are a wonder for their usage of protocols and generics.

I often talked about subject closely related to this one. The use of protocols in swift, how to implement type erasure (for protocol with associated types), introduction to the some keyword when it was about to make its way into the language.

I am a frequent speaker at FrenchKit and I love to dive in complicated topics and try to share something for everyone from novice to experts.

They are no requirement to understand the subject of this talk although there is a lot to take in for someone that knows absolutely nothing about protocols in swift.