Put the ++ and -- operators back at Swift

2018-01-18

Swift had remove the ++ and -- operators for the reason that it might be confusing to users.

Reference: Proposal from Chris Lattner


extension Int {
    static prefix func --(i: inout Int) -> Int {
        i -= 1
        return i
    }
    static prefix func ++(i: inout Int) -> Int {
        i += 1
        return i
    }
    static postfix func --(i: inout Int) -> Int {
        let n = i
        i -= 1
        return n
    }
    static postfix func ++(i: inout Int) -> Int {
        let n = i
        i += 1
        return n
    }
}

Just saw this extension, now we get to use these handy operators yet again. :p