Published on

Introduction to Generics in Swift

According to the Swift Org Docs: Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner.

Introduction

In this article, we’ll learn about generics by writing and optimising a maximumOfTwo function. This function will be used to first get the maximum between two integers but later in this article, you’ll learn how to convert this function into a generic so that this function will work for any two similar types. Let’s get started

Getting started

Open up a Swift playground and write this function definition

func maximumOfTwo(a: Int, b: Int) -> Int {
    return a > b ? a:b
}

This is a rather simple function that takes in two parameters, a and b both of which are of type Int and it returns an Int. Inside this function, we will return the greater value between the two (a, b) by using the ternary operator.

The issue

As we can see, this function only works when the passed values are of type int. This means that you cannot compare any other data types other than Int. For example, you may want to compare two Double or Float values but this function just doesn’t work for those data types in its current form.

The solution

The solution is to convert the maximumOfTwo function into a generic. How do we do that? Using 3 simple steps

  • Introduce a new template type that will conform to Comparable. This is a custom type that we will use to refer to our parameters in the next step.
  • A bit about Comparable: The Comparable protocol is used for types that have an inherent order, such as numbers and strings. Many types in the standard library already conform to the Comparable protocol. Add Comparable conformance to your own custom types when you want to be able to compare instances using relational operators or use standard library methods that are designed for Comparable types. The Comparable protocol allows use to use the comparision operators such as less than, greater than, less than or equal to, greater than or equal to, and equal to, on two variables that conform to this protocol.
func maximumOfTwo<T:Comparable>(a: Int, b: Int) -> Int {
        return a > b ? a:b
}
  • Next, we convert the parameter types to this new type T.
func maximumOfTwo<T:Comparable>(a: T, b: T) -> Int {
        return a > b ? a:b
}
  • Finally, we convert the return type of the function to T.
func maximumOfTwo<T:Comparable>(a: T, b: T) -> T {
        return a > b ? a:b
}

Finishing up

Attempt to call the function now with any type but make sure that the two parameters are of the same type (Since we need to conform to Comparable so that Swift can compare the two values)

Conclusion

In this tutorial we learnt how to get started with generics in Swift by converting a simple function into a generic function to avoid code redundancy and to improve code readability.