Generics and InOut Parameters in Swift with Examples

Lakshmi K
3 min readMar 10, 2022

--

Though generics and inout parameters are 2 different topics , I will cover both of them in this article.

Generics :

Generics allow us to combine multiple functions into one function which will do the same work with different data types , it will allow us to define multiple data types as on data type.

Lets see a case study for generics , we have one function which will swap the two int variables

func swapIntValues(_ a: inout Int , _ b : inout Int) {

let temp = a

a = b

b = temp

}

var int1 = 3

var int2 = 5

swapIntValues(&int1, &int2)

print(int1) // prints 5

print(int2) // prints 3

Let’s take another function which will take two strings and swap them

func swapStringValues(_ a: inout String , _ b : inout String) {

let temp = a

a = b

b = temp

}

var string1 = “Hello”

var string2 = “Welcome”

swapStringValues(&string1, &string2)

print(string1) // prints Welcome

print(string2) // prints Hello

In the above 2 functions , the code is same which will swap the given values, only the data types are different , one is Int and another one is String.

if we want to use the same functionality for other data types like double, etc types instead of creating multiple functions for each data type we can combine them as one function which will accept all the data types.

func swapTwoValues<T>(_ a: inout T, _ b: inout T) { // #1

let temp = a

a = b

b = temp

}

var someInt = 4

var anotherInt = 6

swapTwoValues(&someInt, &anotherInt)

print (someInt) // prints 6

print(anotherInt) // prints 4

var someString = “hello”

var anotherString = “world”

swapTwoValues(&someString, &anotherString)

print (someString) // prints world

print(anotherString) // prints hello

So what is <T> here ?

It’s way of mentioning that this function will accept all the data types

(_ a: inout T, _ b: inout T) — Passing different data type parameters to the function

func swapTwoValues<T> — Defining T in angle brackets after function name is manadatory. With this we are saying T is not any predefined datatype , it’s generic type. So when we declared T type in parameters it won’t give us any errors.

With the above concept we finished basic concept of generics like what is generics and how it works in realtime.

InOut Parameter:

In the above example , we have seen declaring inout before the data type in function declaration , so what is it , why we declared inout ?

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

In function declaration the parameters are constants, it means we can modify them inside the function , we need to assign them to another variables inside the function then we can modify inside parameter. Basically function parameters are constant parameters, like as we declared them with let variable.

func swapTwoValues (_ a : Int , _ b: Int) {

let temp = a

a = b // Compile Time Error: Cannot assign to value: ‘a’ is a ‘let’ constant

b = temp // CompileTimeError: Cannot assign to value: ‘b’ is a ‘let’ constant

}

var someInt = 4

var anotherInt = 6

swapTwoValues(someInt, anotherInt) // only for inout parameters we will pass variables with reference like &someInt and &anotherInt.

So when we have requirement like we need to directly modify the values which we are passing to function then we can use inout functionality.

That’s all for today , We have covered What are generics , basic concept and inout parameter use and declaration. Happy Coding and Reading :)

Check out my other articles https://medium.com/@kn.lakshmi948/dependency-injection-in-swift-with-examples-49d9e09623a8

--

--