SlideShare a Scribd company logo
© 2015 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Swift Custom Operators:
The Good, the Bad and the Ugly
© 2015 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
© 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
© 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
© 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
© 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()
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Syntax
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Game: spot the operator
8
goo.gl/FIPNQ0
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Those are also Operators
• postfix ?, !
• as, as?, as!, is
• …, ..<
• in
• try, try?, try!
9
© 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
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Syntax: Operator Declaration
11
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Let’s define an operator
• Playground time (page DefiningOperator)
12
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Good: Being functional
• Playground time (page BeingFunctional)
13
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad: Usefulness
• Why there’s no such operator before?
15
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Bad: Usefulness
16
• Playground time (page AttributedString)
© 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
© 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
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Ugly
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
The Ugly: Going Nuts
• Playground time (page GoingNuts)
20
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Conclusion
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Curiosities
© 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
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Nice applications
24
• Libraries that are making use of custom operators
• Cartography
• Euler by mattt
• Swift go
• …
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Questions
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Regards to my fellow colleagues
© 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
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
Contact data
Mike Gerasymenko
mihail@gerasimenko.me
(find me on Wire using this email)
mike@wire.com
© 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.

More Related Content

Viewers also liked

Будь здоровым вместе с Традо
Будь здоровым вместе с ТрадоБудь здоровым вместе с Традо
Будь здоровым вместе с Традо
Елена Шальнова
 
Educ6706 literate environment analysis
Educ6706 literate environment analysisEduc6706 literate environment analysis
Educ6706 literate environment analysis
livepeace
 
Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1
Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1
Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1
Kouluterveyskysely
 
Кто выбирает Традо?
Кто выбирает Традо?Кто выбирает Традо?
Кто выбирает Традо?
Елена Шальнова
 
Business Continuity & Disaster Recovery introductie
Business Continuity & Disaster Recovery introductieBusiness Continuity & Disaster Recovery introductie
Business Continuity & Disaster Recovery introductie
B.A.
 
Corso di formazione Export Management
Corso di formazione Export ManagementCorso di formazione Export Management
Corso di formazione Export Management
Octagona Srl
 
Biochip report
Biochip reportBiochip report
Biochip report
Mohit Garg
 
Satya Nadella
Satya NadellaSatya Nadella
Satya Nadella
pritika nuwal
 
How to make your presentation stick
How to make your presentation stickHow to make your presentation stick
How to make your presentation stick
Dr. Mohamed Haggag
 
Поиграем с детьми
Поиграем с детьмиПоиграем с детьми
Поиграем с детьми
tagobr58
 
Teorias organizativas
Teorias organizativasTeorias organizativas
Teorias organizativas
Hellen Velandia
 
Relative pronouns
Relative pronounsRelative pronouns
Relative pronouns
Noelia Contreras
 
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
Агентство Презентаций "Romanoff"
 
Escuela profesional
Escuela profesionalEscuela profesional
Escuela profesional
Pablo Grandez Alvarado
 
Buy or Rent in Columbia SC?
Buy or Rent in Columbia SC? Buy or Rent in Columbia SC?
Buy or Rent in Columbia SC? Clint Hammond
 
Paragraph types
Paragraph typesParagraph types
Paragraph types
Lina Acosta U
 
OpenStack系列公开课2 -20130508
OpenStack系列公开课2 -20130508OpenStack系列公开课2 -20130508
OpenStack系列公开课2 -20130508
OpenCity Community
 

Viewers also liked (20)

Будь здоровым вместе с Традо
Будь здоровым вместе с ТрадоБудь здоровым вместе с Традо
Будь здоровым вместе с Традо
 
Educ6706 literate environment analysis
Educ6706 literate environment analysisEduc6706 literate environment analysis
Educ6706 literate environment analysis
 
Manal p.
Manal p.Manal p.
Manal p.
 
Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1
Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1
Pietilä, Vonk: Terve suu yhteisenä tavoitteena, osa 1
 
Кто выбирает Традо?
Кто выбирает Традо?Кто выбирает Традо?
Кто выбирает Традо?
 
THE COLA WAR
THE COLA WARTHE COLA WAR
THE COLA WAR
 
Business Continuity & Disaster Recovery introductie
Business Continuity & Disaster Recovery introductieBusiness Continuity & Disaster Recovery introductie
Business Continuity & Disaster Recovery introductie
 
Corso di formazione Export Management
Corso di formazione Export ManagementCorso di formazione Export Management
Corso di formazione Export Management
 
Biochip report
Biochip reportBiochip report
Biochip report
 
Satya Nadella
Satya NadellaSatya Nadella
Satya Nadella
 
How to make your presentation stick
How to make your presentation stickHow to make your presentation stick
How to make your presentation stick
 
Поиграем с детьми
Поиграем с детьмиПоиграем с детьми
Поиграем с детьми
 
Teorias organizativas
Teorias organizativasTeorias organizativas
Teorias organizativas
 
Relative pronouns
Relative pronounsRelative pronouns
Relative pronouns
 
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
 
Escuela profesional
Escuela profesionalEscuela profesional
Escuela profesional
 
Buy or Rent in Columbia SC?
Buy or Rent in Columbia SC? Buy or Rent in Columbia SC?
Buy or Rent in Columbia SC?
 
Paragraph types
Paragraph typesParagraph types
Paragraph types
 
Unit 57 terminology becky doyle
Unit 57 terminology becky doyleUnit 57 terminology becky doyle
Unit 57 terminology becky doyle
 
OpenStack系列公开课2 -20130508
OpenStack系列公开课2 -20130508OpenStack系列公开课2 -20130508
OpenStack系列公开课2 -20130508
 

Similar to Custom Swift Operators: The Good, the Bad and the Ugly

Advanced UI styling and animations for iOS
Advanced UI styling and animations for iOSAdvanced UI styling and animations for iOS
Advanced UI styling and animations for iOS
Mike Gerasymenko
 
The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)
The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)
The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)
VMware Tanzu
 
Introduction to Marketing Cloud UI, Adobe Summit 2014
Introduction to Marketing Cloud UI, Adobe Summit 2014Introduction to Marketing Cloud UI, Adobe Summit 2014
Introduction to Marketing Cloud UI, Adobe Summit 2014Damien Antipa
 
Working effectively with OpenShift
Working effectively with OpenShiftWorking effectively with OpenShift
Working effectively with OpenShift
Shekhar Gulati
 
Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...
Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...
Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...
cornelia davis
 
Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017
Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017
Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017
Amazon Web Services
 
Asynchronous Services – A promising future for OSGi - T Ward
Asynchronous Services – A promising future for OSGi - T WardAsynchronous Services – A promising future for OSGi - T Ward
Asynchronous Services – A promising future for OSGi - T Ward
mfrancis
 
Scalable, Available and Reliable Cloud Applications with PaaS and Microservices
Scalable, Available and Reliable Cloud Applications with PaaS and MicroservicesScalable, Available and Reliable Cloud Applications with PaaS and Microservices
Scalable, Available and Reliable Cloud Applications with PaaS and Microservices
David Currie
 
Emulators as an Emerging Best Practice for API Providers
Emulators as an Emerging Best Practice for API ProvidersEmulators as an Emerging Best Practice for API Providers
Emulators as an Emerging Best Practice for API Providers
Cisco DevNet
 
XebiaLabs - Optimizing App Deployment to IBM WebSphere
XebiaLabs - Optimizing App Deployment to IBM WebSphereXebiaLabs - Optimizing App Deployment to IBM WebSphere
XebiaLabs - Optimizing App Deployment to IBM WebSphere
XebiaLabs
 
20100616 Deployit For N How
20100616 Deployit For N How20100616 Deployit For N How
20100616 Deployit For N How
Vincent Partington
 
Running open source PHP applications on you IBM i
Running open source PHP applications on you IBM iRunning open source PHP applications on you IBM i
Running open source PHP applications on you IBM i
Proximity Group
 
Real life-maf-2015
Real life-maf-2015Real life-maf-2015
Real life-maf-2015
Luc Bors
 
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
CloudBees
 
Building a right sized, do-anything runtime using OSGi technologies: a case s...
Building a right sized, do-anything runtime using OSGi technologies: a case s...Building a right sized, do-anything runtime using OSGi technologies: a case s...
Building a right sized, do-anything runtime using OSGi technologies: a case s...
mfrancis
 
Real life-maf-2015-k scope-final
Real life-maf-2015-k scope-finalReal life-maf-2015-k scope-final
Real life-maf-2015-k scope-final
Luc Bors
 
Revive your ERP with APIs and Hybrid Integation Magic by Mikael Sand
Revive your ERP with APIs and Hybrid Integation Magic by Mikael SandRevive your ERP with APIs and Hybrid Integation Magic by Mikael Sand
Revive your ERP with APIs and Hybrid Integation Magic by Mikael Sand
Adam Walhout
 
Authentication and Identity with Amazon Cognito & Analytics with Amazon Pinpoint
Authentication and Identity with Amazon Cognito & Analytics with Amazon PinpointAuthentication and Identity with Amazon Cognito & Analytics with Amazon Pinpoint
Authentication and Identity with Amazon Cognito & Analytics with Amazon Pinpoint
Amazon Web Services
 
Webinar 5 challenges of mobilization april 9 2014
Webinar   5 challenges of mobilization april 9 2014Webinar   5 challenges of mobilization april 9 2014
Webinar 5 challenges of mobilization april 9 2014
Appear
 
JavaOne 2014: Java vs JavaScript
JavaOne 2014:   Java vs JavaScriptJavaOne 2014:   Java vs JavaScript
JavaOne 2014: Java vs JavaScript
Chris Bailey
 

Similar to Custom Swift Operators: The Good, the Bad and the Ugly (20)

Advanced UI styling and animations for iOS
Advanced UI styling and animations for iOSAdvanced UI styling and animations for iOS
Advanced UI styling and animations for iOS
 
The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)
The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)
The Fantastic Voyage to PaaS - Are we there yet? (Cloud Foundry Summit 2014)
 
Introduction to Marketing Cloud UI, Adobe Summit 2014
Introduction to Marketing Cloud UI, Adobe Summit 2014Introduction to Marketing Cloud UI, Adobe Summit 2014
Introduction to Marketing Cloud UI, Adobe Summit 2014
 
Working effectively with OpenShift
Working effectively with OpenShiftWorking effectively with OpenShift
Working effectively with OpenShift
 
Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...
Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...
Devops Enterprise Summit: My Great Awakening: 
Top “Ah-ha” Moments As Former ...
 
Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017
Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017
Testing and Troubleshooting with AWS Device Farm - MBL301 - re:Invent 2017
 
Asynchronous Services – A promising future for OSGi - T Ward
Asynchronous Services – A promising future for OSGi - T WardAsynchronous Services – A promising future for OSGi - T Ward
Asynchronous Services – A promising future for OSGi - T Ward
 
Scalable, Available and Reliable Cloud Applications with PaaS and Microservices
Scalable, Available and Reliable Cloud Applications with PaaS and MicroservicesScalable, Available and Reliable Cloud Applications with PaaS and Microservices
Scalable, Available and Reliable Cloud Applications with PaaS and Microservices
 
Emulators as an Emerging Best Practice for API Providers
Emulators as an Emerging Best Practice for API ProvidersEmulators as an Emerging Best Practice for API Providers
Emulators as an Emerging Best Practice for API Providers
 
XebiaLabs - Optimizing App Deployment to IBM WebSphere
XebiaLabs - Optimizing App Deployment to IBM WebSphereXebiaLabs - Optimizing App Deployment to IBM WebSphere
XebiaLabs - Optimizing App Deployment to IBM WebSphere
 
20100616 Deployit For N How
20100616 Deployit For N How20100616 Deployit For N How
20100616 Deployit For N How
 
Running open source PHP applications on you IBM i
Running open source PHP applications on you IBM iRunning open source PHP applications on you IBM i
Running open source PHP applications on you IBM i
 
Real life-maf-2015
Real life-maf-2015Real life-maf-2015
Real life-maf-2015
 
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
CI and CD Across the Enterprise with Jenkins (devops.com Nov 2014)
 
Building a right sized, do-anything runtime using OSGi technologies: a case s...
Building a right sized, do-anything runtime using OSGi technologies: a case s...Building a right sized, do-anything runtime using OSGi technologies: a case s...
Building a right sized, do-anything runtime using OSGi technologies: a case s...
 
Real life-maf-2015-k scope-final
Real life-maf-2015-k scope-finalReal life-maf-2015-k scope-final
Real life-maf-2015-k scope-final
 
Revive your ERP with APIs and Hybrid Integation Magic by Mikael Sand
Revive your ERP with APIs and Hybrid Integation Magic by Mikael SandRevive your ERP with APIs and Hybrid Integation Magic by Mikael Sand
Revive your ERP with APIs and Hybrid Integation Magic by Mikael Sand
 
Authentication and Identity with Amazon Cognito & Analytics with Amazon Pinpoint
Authentication and Identity with Amazon Cognito & Analytics with Amazon PinpointAuthentication and Identity with Amazon Cognito & Analytics with Amazon Pinpoint
Authentication and Identity with Amazon Cognito & Analytics with Amazon Pinpoint
 
Webinar 5 challenges of mobilization april 9 2014
Webinar   5 challenges of mobilization april 9 2014Webinar   5 challenges of mobilization april 9 2014
Webinar 5 challenges of mobilization april 9 2014
 
JavaOne 2014: Java vs JavaScript
JavaOne 2014:   Java vs JavaScriptJavaOne 2014:   Java vs JavaScript
JavaOne 2014: Java vs JavaScript
 

Recently uploaded

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
karthi keyan
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 

Recently uploaded (20)

Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 

Custom Swift Operators: The Good, the Bad and the Ugly

  • 1. © 2015 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Swift Custom Operators: The Good, the Bad and the Ugly
  • 2. © 2015 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
  • 3. © 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
  • 4. © 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
  • 5. © 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
  • 6. © 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()
  • 7. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Good: Syntax
  • 8. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Game: spot the operator 8 goo.gl/FIPNQ0
  • 9. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Those are also Operators • postfix ?, ! • as, as?, as!, is • …, ..< • in • try, try?, try! 9
  • 10. © 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
  • 11. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Good: Syntax: Operator Declaration 11
  • 12. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Good: Let’s define an operator • Playground time (page DefiningOperator) 12
  • 13. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Good: Being functional • Playground time (page BeingFunctional) 13
  • 14. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Bad
  • 15. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Bad: Usefulness • Why there’s no such operator before? 15
  • 16. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Bad: Usefulness 16 • Playground time (page AttributedString)
  • 17. © 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
  • 18. © 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
  • 19. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Ugly
  • 20. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. The Ugly: Going Nuts • Playground time (page GoingNuts) 20
  • 21. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Conclusion
  • 22. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Curiosities
  • 23. © 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
  • 24. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Nice applications 24 • Libraries that are making use of custom operators • Cartography • Euler by mattt • Swift go • …
  • 25. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Questions
  • 26. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.
  • 27. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Regards to my fellow colleagues
  • 28. © 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
  • 29. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL. Contact data Mike Gerasymenko mihail@gerasimenko.me (find me on Wire using this email) mike@wire.com
  • 30. © 2014 WIRE SWISS GMBH. PROPRIETARY AND CONFIDENTIAL.