jweinst1
6/3/2016 - 6:25 PM

using protocols with self in swift

using protocols with self in swift

//protocol using explicit parameter Self, for multiple adaptation
protocol adderMachine {
    mutating func add(arg:Self) -> Void
    func getval() -> Self
}

struct num:adderMachine {
    var int:Int
    init() {
        self.int = 0
    }
    //allows same type to be argued with it
    mutating func add(arg:num) -> Void {
        self.int += arg.int
    }
    //returns a new version of itself
    func getval() -> num {
        var result = num()
        result.int = self.int
        return result
    }
}