Protocol Composition — Swift

--

Photo by Ricardo Gomez Angel on Unsplash

While working with protocols in swift, you might have written a type conforming to multiple protocols.
Here comes Protocol Composition a technique to combine multiple protocols into a single representation.

How it is written:

The problem with it is if there are too many protocols then the conformance code looks weird.

Better way of representing it is using typealias, like this;

The most common example of protocol-composition you might have used is Codable protocol; yes it is a composition of two different protocols Encodable and Decodable.

Thanks for reading.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sudhanshu Srivastava
Sudhanshu Srivastava

Written by Sudhanshu Srivastava

I write about what I learn. Swift, Kotlin, Flutter, Social and Life.

No responses yet

Write a response