custom swift operators: the good, the bad and the ugly
TRANSCRIPT
![Page 1: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/1.jpg)
© 2015 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Swift Custom Operators: The Good, the Bad and the Ugly
![Page 2: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/2.jpg)
© 2015 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
![Page 3: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/3.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
About the Author: Mike Gerasymenko
• Working on iOS since 2008 (iOS 2.2)
• Around 35 apps for the AppStore
• With Wire since March 2014
3
![Page 4: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/4.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Preface: Terminology
• Operator describes operation, which is a calculation from zero or more input values (operands) to an output value.
• Arity of operators:
• unary: !x, x++, x--, x~, --x, ++x
• binary: x+y, x-y
• ternary: x? a : b
• n-ary • Placement
• Prefix: !x
• Infix: x+y
• Postfix: x++
4
![Page 5: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/5.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Preface: Precedence and Associativity
• During the evaluation of the expression, first the operators with the higher precedence are being evaluated, for example:
• x = a + b * c is x = a + (b * c)
• Given that the precedence is equal for the operators, the operators are evaluated according to their associativity, left, right or non-associative. Having left associativity means that the operations are performed from left to right, for example:
• x = a + b + c is x = (a + b) + c
5
![Page 6: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/6.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Preface: Extras
• Overloading means that the same operator can work with different types:
• operator+ can be applied to Int, Double, …
• Short-circuit evaluation means that operand is only calculated when necessary:
• evaluation of false && obj.isTrue() would not invoke obj.isTrue()
![Page 7: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/7.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Syntax
![Page 8: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/8.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Game: spot the operator
8
goo.gl/FIPNQ0
![Page 9: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/9.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Those are also Operators
• postfix ?, !
• as, as?, as!, is
• …, ..<
• in
• try, try?, try!
9
![Page 10: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/10.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Syntax
• Swift is really flexible with operator definition:
• Operator definition consists of two parts: operator declaration and at least one function that gives the meaning to an operator
• Arity, placement, precedence and associativity can be defined per operator
• Rich set of operators that could be defined (standard operation characters + UTF8)
• Swift gives the ability to overload almost any existing operator (except is, as, as?, =, ??, prefix &, postfix ?, ! and «? :»)
10
![Page 11: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/11.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Syntax: Operator Declaration
11
![Page 12: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/12.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Let’s define an operator
• Playground time (page DefiningOperator)
12
![Page 13: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/13.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Being functional
• Playground time (page BeingFunctional)
13
![Page 14: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/14.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad
![Page 15: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/15.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad: Usefulness
• Why there’s no such operator before?
15
![Page 16: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/16.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad: Usefulness
16
• Playground time (page AttributedString)
![Page 17: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/17.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad: Understanding of new operators
• New operator could be puzzling for other developers
• Overloading an existing operator could give a hard time debugging the code where it is not clear that new overloaded implementation is used
17
![Page 18: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/18.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad: Global Scope
• Whatever you define, it would appear at the global scope.
• Operators from frameworks are visible in user code
• Being in global scope makes it easy to collide implementing same operator in the different way in framework and in user code
18
![Page 19: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/19.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Ugly
![Page 20: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/20.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Ugly: Going Nuts
• Playground time (page GoingNuts)
20
![Page 21: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/21.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Conclusion
![Page 22: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/22.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Curiosities
![Page 23: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/23.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Non-associative
• operator== is non-associative, so it is harder to shoot your own foot:
• For example, x == y == z in C would evaluate as (x == y) == z, and not to x == y, y == z
23
![Page 24: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/24.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Nice applications
24
• Libraries that are making use of custom operators
• Cartography
• Euler by mattt
• Swift go
• …
![Page 25: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/25.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Questions
![Page 26: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/26.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
![Page 27: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/27.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Regards to my fellow colleagues
![Page 28: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/28.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
References
• The Swift Programming Language https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
• Facets of Swift, Part 5: Custom Operators https://medium.com/swift-programming/facets-of-swift-part-5-custom-operators-1080bc78ccc#.l40cmmsy2
• Functional Programming in Swift http://five.agency/functional-programming-in-swift/
• Railroad Diagram Generator http://www.bottlecaps.de/rr/ui
• 100 Most Popular Cat Names https://www.cuteness.com/popular-cat-names
28
![Page 29: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/29.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Contact data
Mike Gerasymenko
[email protected] (find me on Wire using this email)
![Page 30: Custom Swift Operators: The Good, the Bad and the Ugly](https://reader034.vdocuments.us/reader034/viewer/2022042907/58cee6a21a28ab333d8b5329/html5/thumbnails/30.jpg)
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.