9 Comments

  • Using defer

    • In your case:

      postfix public func ++(inout x: Int) -> Int { defer { x += 1 }; return x }

      • Sold! Thanks

  • Weird, but interesting:

    postfix operator +++ {}
    prefix operator +++ {}
    
    postfix public func +++<T: IntegerArithmeticType>(inout x: T) -> T { defer { x += x/x }; return x }
    prefix public func +++<T: IntegerArithmeticType>(inout x: T) -> T { x += x/x; return x }
    
    prefix public func +++(inout x: CGFloat) -> CGFloat { x += 1; return x }
    
    // results
    var a = 5
    var b: UInt8 = 10
    var c: CGFloat = 20
    
    +++a
    +++b
    +++c
    
    • Your validator erased generic declaration.

      i.e.:
      postfix public func +++^T: IntegerArithmeticType^(inout x: T) -> T { defer { x += x/x }; return x }

  • x += x/x looks like a cool workaround, but what if x == 0?

    How about making it:

    postfix public func +++(inout x: T) -> T { defer { x = x.successor() }; return x }

    How about performance? What ist the curent ++ postfix compared to such a generic function in Swift 3?

    • x += x/x โ€“ as I noticed, it is just case not for using.

      x.successor it’s good case of course ๐Ÿ˜‰

  • damn parser… it was supposed to read:

    postfix public func + + + etc.

  • !?$$&ยง*

    It shall read IntegerType inside the angle brackets for the protocol requirements of the function declaration…