That being said, Generics got its share of extra attention and shot into the limelight due to WWDC 2022.
Now, coming back to the highlights of WWDC 2022, Apple announced exciting new additions to Generics as part of Swift 5.7, leading to it getting its day in the sun.
So, what are these new additions to Generics in Swift 5.7 that got developers excited?
It’s also important to note that these were released with Xcode version 14. So, let’s explore these new additions with our pizza shop example; you remember that, don’t you?
An associated type gives a placeholder name to a type that is used as part of the protocol. Using our pizza shop example, we start by creating PizzaTopping enum that will have various pizza toppings.
Next, we create a protocol RestaurantItem that will have an associatedtype Message, which has a default type of String. Also, we have defined two methods — acceptOrderItem and prepareOrderItem, and both return Message type.
We will use this RestaurantItem protocol to create concrete products. For our example, we will also create PizzaType, SideDishType, and DrinkType types that conform to RestaurantItem.
Here, we’ll create a concrete product for three types of pizza — Margherita, farmhouse, and golden corn. We start by creating an enum PizzaType that conforms to String and RestaurantItem protocol while defining three cases for the three types of pizzas. Additionally, a variable type called toppings is introduced, which will hold an array of PizzaTopping depending on the case of the enum instance.
We also need to implement the two methods from RestaurantItem protocol, acceptOrderItem, and prepareOrderItem. This is because these functions will form an appropriate string based on the pizza selection and return the String.
Similarly, we also create a SideDishType that conforms to String and RestaurantItem protocol.
Furthermore, we also define two cases for fries and calzone while implementing the acceptOrderItem and prepareOrderItem methods.
Continuing in the same vein, we create DrinkType that conforms to String and RestaurantItem protocol.
We also define two cases for soda and lemonade while implementing the acceptOrderItem and prepareOrderItem methods.
Here, we have created a new class called PizzaShop. To proceed, we also declare a variable orderNumber to keep track of the order number.
To attract customers to our pizza store, we launch an offer for customers, where they can buy one item and get the second for free. To do this, we create a method called onePlusOneOffer, which accepts some restaurant parameter. Before the launch of Swift 5.7, this feature wasn’t available, so let’s thank the Swift gods 😆
This method will accept any type that conforms to RestaurantItem. Once this is done, we can just print the output of acceptOrderItem and prepareOrderItem twice within a loop. Additionally, we also increase the order number and print the order start and order complete messages.
The pizza shop also needs another method that can accept a happy meal-like combo that includes a pizza, a side dish, and a drink in one order. It should also be able to create a custom order of any combination of restaurant items. To do this, we create a single method called mealOrder. This method will accept an array of any RestaurantItem. This capability was also not present before Swift 5.7.
Within this method, we can also loop through the array and print the acceptOrderItem and PrepareOrderItem messages.
The third new feature in Generics as part of Swift 5.7 allows the compiler to unwrap the concrete type from any abstract type definition and call the correct concrete type implementation. Thus, we can print the start and end of the order while increasing the orderNumber property.
Now, we’ll create two constants of any RestaurantItem array for the happy meals. We instantiate PizzaShop class and call the mealOrder method and pass the happy meal constants or any custom order array.
And, when we call the onePlusOneOffer method and pass an order for any item in the parameter, we can get one extra item with each item ordered.
With the aforementioned updates to Generics in Swift 5.7, quite a few limitations have been lifted, making it more relevant and usable in Swift codebases.
Now, you can go forth and make Generics prosper in Swift using these features. Just don’t use our pizza shop example 😆
The story around Swift doesn’t end here, as we conclude the Swift series with an exciting sneak peek into the intriguing world of SwiftUI charts, so stay tuned!