Updated 1 November 2020
Generics provides the powerful features to write flexible, reusable functions, remove bulky code and work with any type. Here, we can remove duplication of the code of different data types in an abstracted manner. In Swift, Array and Dictionary are of the generic type. Both store Int values, String values, Double values, Float values, Bool values which you want.
Let’s compare normal and generic functions. Normal function example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
func stringFunc(_ firstString: String, _ secondString: String) { //Used String Parameter } func intFunc(_ firstInt: Int, _ secondInt: Int) { //Used Int Parameter } func doubleFunc(_ firstDouble: Double, _ secondDouble: Double) { //Used Double Parameter } func arrayDictFunc(_ firstArray: [Double], _ secondDictionary: [String:Any]) { //Used Array and Dictionary Parameter } |
You have noticed that the bodies of the stringFunc(_:, _:), doubleFunc(_:, _:), intFunc(_:, _:), and arrayDictFunc(_:, _:) functions may work identically, but then there is the only difference of types of values The only difference is the type of the values that they accept (Int
, String
, and Double
).
It will be more helpful if we write a single function to replace all normal functions and work for every type of value in a single function.
For this, swift introduce the Generics to provide powerful functionality and more flexibility in the coding. Generic function example:
1 2 3 |
func genericFunc<T>(_ a: T, _ b: T){ // Used Parameter of any type } |
The generic version of the function uses a placeholder type name (called T
, in this case) instead of an actual type name (such as Int
, String
, or Double
). The placeholder type name doesn’t say anything about what T
must be, but it does say that both a
and b
must be of the same type T
, whatever T
represents. The actual type to use in place of T
is determined each time.
And T
is inferred to be different types as:
1 2 3 4 5 6 7 8 9 10 11 |
//Int Values genericFunc(1, 1) //Double Values genericFunc(8796789789, 134985978465.3454) //String Values genericFunc("Siddhant", "Ranjit") //Array Values genericFunc([1,2,3,4], [5,6,7,8]) |
If the generic function has two or more different parameter in the same function then we used different placeholder like,
1 2 3 |
func genericFunc<T,K>(_ a: T, _ b: K){ } |
T and K can infer to different types as:
1 2 3 4 5 6 7 8 9 10 11 |
//Int & String Values genericFunc(1, "Vicky") //Double & Int Values genericFunc(8796789789.78, 10) //String & Int Values genericFunc("Siddhant", 4) //Array & Dictionary Values genericFunc([1,2,3,4], ["key1": "Value 1"]) |
Generics are at the core of many common language features, such as arrays and dictionaries. It is an integral feature that you’ll use every day to write powerful and type-safe abstractions.
So if you have any comments, questions, or recommendations, feel free to post them in the comment section below!
…..
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.