Making an operator a convenience wrapper around an existing function or method provides additional flexibility to users in how the functionality is
called and in what options are passed in.
This rule raises an issue when the function that defines the operation of a operator consists of something other than a single function call.
Noncompliant code example
infix operator >< { associativity right precedence 90 }
func >< (left: Double, right: Double) -> Double { // Noncompliant
let leftD = (left % 1) * 100
let rightD = (right % 1) * 100
let leftW = (left - leftD) / 100
let rightW = (right - rightD) / 100
return (leftD + leftW) * (rightD + rightW)
}
Compliant solution
infix operator >< { associativity right precedence 90 }
func >< (left: Double, right: Double) -> Double {
return fubar(left, right)
}
func fubar(left: Double, right: Double) -> Double {
let leftD = (left % 1) * 100
let rightD = (right % 1) * 100
let leftW = (left - leftD) / 100
let rightW = (right - rightD) / 100
return (leftD + leftW) * (rightD + rightW)
}
Exceptions
Operators that end with =
are expected to update their left-hand operands, and are therefore ignored.
func **= (inout p1:Int, p2:Int) {
p1 = p1 ** p2
}