What exactly are Generics

In fact Array and Dictionary are generic collections, this is why you can create an Array of Int , String or any other type

What are Generics



Generics are one of the most powerful features of Swift, It allows you to create flexible Functions and Types that can work with any type.

If you used Array or Dictionary before in Swift then you already knows about Generics.
In fact Array and Dictionary are generic collections, this is why you can create an Array of Int , String or any other type and the same with Dictionary.

Why use Generics

Imagine that you need to create a function that swaps two variables of type Int like this:

    func swapInts(_ a: inout Int, _ b: inout Int) {
        let temporaryA = a
        a = b
        b = temporaryA
    }

Now what if you want to create another function to swap the values of two Strings, you will have to do it like this:

    func swapStrings(_ a: inout String, _ b: inout String) {
        let temporaryA = a
        a = b
        b = temporaryA
    }

You may also need to swap doubles so you will create a third function and so on.

You might ask yourself, isn’t there a way to create one function that can swap the values of the two parameters whether they are Ints or Strings or anything else?

The answer is YES there is a very simple way to do that using a Generic Function.

Generic Function Example

func swapTwoValues<T>(_ a: inout T, _ b: inout T) {
    let temporaryA = a
    a = b
    b = temporaryA
}

To create a Generic Function you have to write a placeholder type between two angle brackets after the function name – in this case we used <T> – then you can use it as a type for the parameters or return type.

The actual type is determined every time you call the function.

In the above function we used <T> but you can use anything else like, <X> or <MyType>

we call it like any other function

var someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)

var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)

Generic Type Example

We can also define a Generic Type which is a custom Class or Structure or Enumeration just like Array and Dictionary

struct IntStack {
    var items = [Int]()
    mutating func push(_ item: Int) {
        items.append(item)
    }
    mutating func pop() -> Int {
        return items.removeLast()
    }
}

That was a stack object – Last In First Out- we defined that accepts only Int types.

We can define a Generic version of it so that we can add any type like String or Int or Double

struct Stack<Element> {
    var items = [Element]()
    mutating func push(_ item: Element) {
        items.append(item)
    }
    mutating func pop() -> Element {
        return items.removeLast()
    }
}

Then you create an instance of  it by writing the type you will store in it inside angle brackets

var stackOfStrings = Stack<String>()

stackOfStrings.push("tres")
stackOfStrings.push("cuatro")

If you don’t know what are InOut Parameters you can read about them here

Leave a Reply

Your email address will not be published. Required fields are marked *