Slide 1

Slide 1 text

Operators in Swift Sash Zats - zats.io - @zats - September 8, Tel Aviv

Slide 2

Slide 2 text

Operator?

Slide 3

Slide 3 text

Operator! Language constructs which behave generally like functions, but differ syntactically or semantically from usual functions — Wikipedia

Slide 4

Slide 4 text

…in Swift? An operator is a special symbol or phrase that you use to check, change, or combine values. — The Swift Programing Language

Slide 5

Slide 5 text

…in Swift! • Assignment, logic, math, binary, overflow, range; • Unary, binary, ternary; • Prefix, infix, postfix; • Precedence • Associativity

Slide 6

Slide 6 text

Overloading & custom operators • Define operator • Associativity, assignment, precedence • Implement function • Arguments define applicability • Overrides operators within the module • inout for assignment

Slide 7

Slide 7 text

Guidelines • Clarity • Simplicity • Analogy • Order of execution

Slide 8

Slide 8 text

Demo

Slide 9

Slide 9 text

Shortcoming • Why bother? What's wrong with functions? • No unicode in operator names. Where is my image.!? • No way to create ternary operators • No scopes: Cartography - auto layout done with custom operators

Slide 10

Slide 10 text

Conclusion To get around clashes when operator overloading, we should all just give them a three letter prefix — @danielctull