Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diffrence between Function and Generic Function in swift

Tags:

swift

xcode6

ios8

i want to know the difference between the Function and Generic function in swift.Following Function and Generic function is doing same..can anyone tell me the exact use of Generic functions ?

func simpleMin<T: Comparable>(x: T, y: T) -> T {  //Generic functions
    if x < y {
        return y
    }
    return x
}


func sampleMin(x:AnyObject,y:AnyObject)->AnyObject{  //Function
    if x.integerValue < y.integerValue {
        return y
    }
    return x
}
like image 289
Mani murugan Avatar asked Mar 19 '23 05:03

Mani murugan


1 Answers

Generic functions let you use the type safety of Swift on both the parameters and the result of the function to write safer, cleaner code. For example, your first function requires that both parameters passed in be of the same type, and guarantees a return value of that same type:

let minInt: Int = simpleMin(5, 12)
let minDouble: Double = simpleMin(5.0, 12.0)

whereas your second function makes no such requirements and no such guarantee:

let minOne: AnyObject = sampleMin(5, 12.0)     // minOne is an AnyObject holding an Int
let minTwo: AnyObject = sampleMin(5.0, 12)     // minTwo is an AnyObject holding an Double
let minThree: AnyObject = sampleMin("Five", true)   // what is supposed to happen here, exactly?

With these AnyObject results I would need to do extra checks to make sure I understand what the function returned, since AnyObject is (obviously) much less specific than my original parameters.

Moreover, generic functions allow you to put constraints on the parameters they accept, so you can make sure that the function is called only with arguments that make sense. Your first function requires that the parameters conform to the Comparable protocol, meaning that I can't just call it with two random objects. The compiler will let me call your second function with two instances of UIView, for example, and there won't be any problem until the crash when that code is executed.


Understanding protocols is an important part of using generics. A protocol defines some specific functionality that would be useful across a whole range of classes, and leaves the implementation of that functionality up to the classes themselves. The Comparable protocol above is one example; here's the definition:

protocol Comparable : Equatable {
    func <=(lhs: Self, rhs: Self) -> Bool
    func >=(lhs: Self, rhs: Self) -> Bool
    func >(lhs: Self, rhs: Self) -> Bool
}

This is saying that any object that "conforms to" the Comparable protocol is going to have definitions for using the <=, >=, and > operators -- that is, you'd be able to write if a > b for any two objects that are both Comparable.


More reading:
The Swift Programming Language: Generics
Generic Programming - Wikipedia

Generics have long been a feature of C# and Java, among other languages, so you may find more resources to help you understand their benefits and how to use them in their documentation.

like image 193
Nate Cook Avatar answered Mar 31 '23 19:03

Nate Cook