Chapter 6. Maximization of Code Reuse with Generic Code

In this chapter, you will learn about parametric polymorphism and how Swift implements this object-oriented concept through the possibility of writing generic code. We will use classes that work with one and two constrained generic types.

In addition, you will learn to combine the generic code with inheritance and multiple inheritance to demonstrate the usage of generic code in real-life situations, in which the code becomes more complex than the usage of a simple generic class.

Understanding parametric polymorphism and generic code

Let's imagine we want to organize a party for specific animals. We don't want to mix cats with dogs because the party would end up with the dogs chasing cats. We want a party, and we don't want intruders. However, at the same time, we want to take advantage of the procedures we create to organize the party and replicate them with frogs in another party; it would be a party of frogs. We want to reuse the procedures for either dogs or frogs. However, in future, we will probably want to use them with other animals, such as parrots, lions, tigers, and horses.

In the previous chapter, you learned how to work with protocols. We can declare a protocol to specify the requirements for an animal and then take advantage of Swift features to write a generic code that works with any class that implements the protocol. Parametric polymorphism allows us to write generic and reusable code that can work with values without depending on the type, while keeping the full static-type safety.

We can take advantage of parametric polymorphism in Swift through generics, also known as generic programming. Once we declare a protocol that specifies the requirements for an animal, we can create a class that works with any instance that conforms to this protocol. This way, we can reuse the code that generates a party of dogs and create a party of frogs, parrots, or any other animal, that is, a party of any instance of a class that conforms to the animal protocol.

Tip

Other strongly typed programming languages, such as C# and Java, allow us to work with parametric polymorphism through generics. In case you've worked with these programming languages, you will find that the Swift syntax is very similar. The main difference is that Swift uses protocols instead of interfaces.

Other programming languages work with a different philosophy known as duck typing, where the presence of certain attributes or properties and methods make an object suitable to its usage as a specific animal. With duck typing, if we require animals to have a name property and we provide sing and dance methods, we can consider any object an animal as long as it provides the required name property and both the sing and dance methods. Any instance that provides the required property and methods can be used as an animal.

Let's think about the following situation: we see a bird. The bird quacks, swims, and walks like a duck, so we can call this bird a duck. Very similar examples related to a bird and duck generate the duck typing name. We don't need additional information to work with the bird as a duck. Python, JavaScript, and Ruby are examples of languages where duck typing is extremely popular.

Tip

We can also work with duck typing in Swift. However, it requires many workarounds, and it is not the most natural way of working in Swift. Thus, we will focus our efforts on writing a generic code with parametric polymorphism through generics.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.78.157