From the course: Swift 5: Protocol-Oriented Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

The importance of generics

The importance of generics

From the course: Swift 5: Protocol-Oriented Programming

Start my 1-month free trial

The importance of generics

- [Instructor] Although this course is about protocol-oriented programming, we can't avoid talking about generics. Generics are deeply rooted in the language. We usually don't even notice that we're relying on them. Whenever we encounter protocols, we'll often also see generics. As an example, the built-in Swift collection types conform to the collection protocol and several other protocols as well. Besides, they are also implemented as generic types. The dictionary, the array and the set, are all defined as generic types. So, why do we use generics? In short, generics can save us from typing similar code over and over again. I show you what that means through a practical example. Let's assume that we need a function that tests whether two values are equal. We could come up with the following solution. This implementation is straightforward and we can test it by typing equals and for the left hand side argument, I…

Contents