Generic Function- in Swift

Yuvraj Kale
3 min readApr 19, 2020

Many developers having a problem with understanding the concept generic.
So here I am trying to give some clear idea about it in simple words.

What is Generic meaning in normal life?

To understand generic we need to understand what is generic in our day to day life.
The definition of generic is that its a product without a brand name. means you have only idea about its functionality but don’t know from what it is made of.

What is Generic in Swift?

It’s one of the most powerful features from swift. Because of generic, you can write flexible and reusable code. I would like to say generic is a universal data type in swift. It means you don’t decide the data type of it. Yes, you read it right it’s universal data type because you don’t need to say what type of variable it is. It automatically adapts the properties of the assigned data type.
For better understanding, we can dive into examples directly.

let’s say you are writing a nongeneric function which swaps two values of two variables.

Example 1:

func swapTwoInt(_ first: inout Int, second: inout Int){
let temp = first
first = second
second = temp
}

The above function is used to swap values of two int type variable.

I have used the in-out parameter because I can’t change the value of parameters passed in the function.

Now, what if I want to swap two string values? For that, I need to write another function for the same.

Example 2:

func swapTwoString(_ first: inout String, second: inout String){
let temp = first
first = second
second = temp
}

Above function swap values of two String type variable.
And then what if we want to swap two double type values and many more. So writing different functions for different data types is not good programming practice.

So only for different data types, we are using two different functions. If this is the case how can we say ourselves as programmer? And here Generic function came to rescue our efforts.

Generic function is the function that accepts any data type as it does not declare which data type it’s going to accept while it’s declaration.

Here is the example for your understanding

func swapTwoValues<T>(_ first: inout T, second: inout T){
let temp = first
first = second
second = temp
}

In this generic function definition, you are seeing T. Now that T is a placeholder for the actual data type like String, Int, Double, etc.

You can use anything instead of T. But what you write in angular brace i.e <> is used everywhere in place of T.

There is only one difference in Normal function and generic function definition and that is <T>.

func swapTwoInts(_ a: inout Int, _ b: inout Int)
func swapTwoValues<T>(_ a: inout T, _ b: inout T)

How can I make a call of generic function?

Up to definition and declaration, everything is clear about generic function but how can I make call of generic function? Simple, Generic function can be called the same as how you call nongeneric function.

In fact, you’ve been using generics throughout the Language Guide, even if you didn’t realize it. For example, Swift’s Array and Dictionary types are both generic collections. You can create an array that holds Int values, or an array that holds String values, or indeed an array for any other type that can be created in Swift.

--

--