Swift 自定义运算符 ++ -- **

Swift 自定义运算符 ++ -- **

extension Int {
    static prefix func ++ (left:inout Int) -> Int{
        left += 1;
        return left;
    }
}

extension Int {
    static postfix func ++ (left:inout Int) -> Int{
        left += 1;
        return left - 1;
    }
}

prefix operator &++
extension Int {
    static prefix func &++ (left:inout Int) -> Int{
        left &+= 1;
        return left;
    }
}

postfix operator &++
extension Int {
    static postfix func &++ (left:inout Int) -> Int{
        left &+= 1;
        return left &- 1;
    }
}

extension Int {
    static prefix func -- (left:inout Int) -> Int{
        left -= 1;
        return left;
    }
}

extension Int {
    static postfix func -- (left:inout Int) -> Int{
        left -= 1;
        return left + 1;
    }
}

prefix operator &--
extension Int {
    static prefix func &-- (left:inout Int) -> Int{
        left &-= 1;
        return left;
    }
}

postfix operator &--
extension Int {
    static postfix func &-- (left:inout Int) -> Int{
        left &-= 1;
        return left &+ 1;
    }
}




infix operator ** : MultiplicationPrecedence
extension Int {
    static func ** (left:Int, right:Int) -> Int{
        var result = 1;
        for _ in 0.. Int{
        var result = 1;
        for _ in 0..

测试结果:

var a = 10
print(a++)
print(a)

a = 10
print(++a)
print(a)

a = 10
print(a--)
print(a)

a = 10
print(--a)
print(a)

a = 10
print(a ** 10)

a = Int.max
print(a&++)
print(a)

a = Int.max
print(&++a)
print(a)

a = Int.min
print(a&--)
print(a)

a = Int.min
print(&--a)
print(a)

a = 2
print(2 &** 63)

运行结果:
10
11
11
11
10
9
9
9
10000000000
9223372036854775807
-9223372036854775808
-9223372036854775808
-9223372036854775808
-9223372036854775808
9223372036854775807
9223372036854775807
9223372036854775807
-9223372036854775808

你可能感兴趣的:(Swift 自定义运算符 ++ -- **)