When programming in Objective C I used to regularly use the == as if and only if. For example if you want to check that two doubles have the same sign the following code does nicely:

a < 0 == b < 0

This works because the == operator has a lower precedence than the < operator, so the language knows that it should compute the result of < before it computes the result of ==.

Unfortunately in swift this no longer works as smoothly because all comparison operators have the same precedence: ComparisonPrecedence. This means that the equivalent code in Swift looks like this:

(a < 0) == (b < 0)

Whilst this isn’t a big deal for me it means getting a warning and having to go back and fix the problem. So I thought it may be fun to play with Operators in Swift. Here is the resulting code:

precedencegroup IffPrecedence {
higherThan: LogicalConjunctionPrecedence
lowerThan: ComparisonPrecedence
associativity: none
assignment: false
}
infix operator <=> : IffPrecedence
public func <=>(left : Bool, right:Bool) -> Bool {
return left == right
}

It is then possible to use <=> in my code, which has the desired precedence, and as an added bonus has a symbol that looks like the $\Leftrightarrow$ symbol. This means that I don’t have to remember that == does the same thing as $\Leftrightarrow$ when using booleans. Note that this is not an attempt to replace the == operator, more an attempt to introduce the predicate operator of ‘if and only if’. I don’t keep up to date enough with Swift etiquette to know whether this is considered bad!