CocoaheadsSKG
Dimitri James Tsiflitzis
Source: https://developer.apple.com/reference/swift/swift_standard_library_operators
let a = 3
let b = 4
let c = a * a + b * b // c = (a * a) + (b * b)
let d = b — a + b // d = (b - a) + bOperators have a precedence, and an associativity that determine the order in which they are executed.
var a = false
var b = false
var c = true
if a == b == c { // COMPILER ERROR
print("WAT")
}Operators can either be left-associative, right-associative, or non-associative.
func foo() -> Bool {
print("foo() called")
return true
}
false && foo()A couple of operators in Objective-C and Swift provide short-circuit evaluation. That means an operand is only calculated if necessary.
*I once impressed an interviewer with this one
func * (left: String, right: Int) -> String {
if right <= 0 {
return ""
}
var result = left
for _ in 1..<right {
result += left
}
return result
}
"a" * 6
// "aaaaaa"Swift has the ability to overload operators such that existing operators, like +
func +(left: [Double], right: [Double]) -> [Double] {}
func <<<T> ( left: inout [T], right: T) -> [T] {
left.append(right)
return left
}Consider the arithmetic operator found in many programming languages, but missing in Swift **, which raises the left hand number to the power of the right hand number
infix operator ** : MultiplicationPrecedence*command click import swift to check precedence
infix operator **
func ** (left: Double, right: Double) -> Double {
return pow(left, right)
}
2 ** 3
// 8When creating custom operators, make sure to also create the corresponding assignment operator, if appropriate:
infix operator **=
func **= ( left: inout Double, right: Double) {
left = left ** right
}Or consider the ± operator, which can be used either as an infix or prefix to return a tuple with the sum and difference:
infix operator ±
func ± (left: Double, right: Double) -> (Double, Double) {
return (left + right, left - right)
}
prefix operator ± {}
prefix func ± (value: Double) -> (Double, Double) {
return 0 ± value
}
2 ± 3
// (5, -1)
±4
// (4, -4)Consider a set union ∪
let set1: Set<Int> = [1, 5]
let set2: Set<Int> = [2, 5]
let set1: set1 ∪ set2
print(set3) // [1, 2, 5]precedencegroup PowerMultiplicationPrecedence {
lowerThan: MultiplicationPrecedence
associativity: left
}
infix operator **: PowerMultiplicationPrecedence
// in < 2.3
infix operator ** { associativity left precedence 160 }Lesson One
Operators are precious and expensive. They involve significant mental costs for creating code for reading
Lesson Two
An operator that is sourced from foundational concepts like math establishes important context and association cues.
Lesson Three
If you’re going to dive in and use an operator, select non-trivial functionality with the biggest impact possible.
Lesson Four
A great operator that’s impossible to type isn’t going to get used.
Lesson Five
Think global. First, prefer symbols that can be as easily entered on the keyboard.
Lesson Six
When you adopt an operator use it, use it.
Lesson Seven
Prefer usable code to pretty code.
Further reading
https://news.realm.io/news/slug-erica-sadun-operators-strong-opinions/
http://nshipster.com/swift-operators/
https://medium.com/swift-programming/facets-of-swift-part-5-custom-operators-1080bc78ccc