My first experience with generics was back in 2004, when they were first introduced in the Java programming language. I can still remember picking up my copy of The Java Programming Language, Fourth Edition, which covered Java 5 and reading about Java's implementation of generics. Since then, I have used generics in several projects, not only in Java but also in other languages. If you are familiar with generics in other languages, such as Java, the syntax that Swift uses will be very familiar to you. Generics allow us to write very flexible and reusable code; however, just like with subscripts, we need to make sure that we use them properly and do not overuse them.
In this chapter, we will cover the following topics:
- What are generics?
- How to create and use generic functions?
- How to create and use generic types?
- How to use associated types with protocols?