SlideShare une entreprise Scribd logo
1  sur  30
Télécharger pour lire hors ligne
© 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.

Contenu connexe

En vedette

Будь здоровым вместе с Традо
Будь здоровым вместе с ТрадоБудь здоровым вместе с Традо
Будь здоровым вместе с ТрадоЕлена Шальнова
 
Educ6706 literate environment analysis
Educ6706 literate environment analysisEduc6706 literate environment analysis
Educ6706 literate environment analysislivepeace
 
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 1Kouluterveyskysely
 
Business Continuity & Disaster Recovery introductie
Business Continuity & Disaster Recovery introductieBusiness Continuity & Disaster Recovery introductie
Business Continuity & Disaster Recovery introductieB.A.
 
Corso di formazione Export Management
Corso di formazione Export ManagementCorso di formazione Export Management
Corso di formazione Export ManagementOctagona Srl
 
Biochip report
Biochip reportBiochip report
Biochip reportMohit Garg
 
How to make your presentation stick
How to make your presentation stickHow to make your presentation stick
How to make your presentation stickDr. Mohamed Haggag
 
Поиграем с детьми
Поиграем с детьмиПоиграем с детьми
Поиграем с детьмиtagobr58
 
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...
Презентация обувного магазина "La grange" Презентация для входа в ТЦ. Презент...Агентство Презентаций "Romanoff"
 
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
 
OpenStack系列公开课2 -20130508
OpenStack系列公开课2 -20130508OpenStack系列公开课2 -20130508
OpenStack系列公开课2 -20130508OpenCity Community
 

En vedette (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
 

Similaire à 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 iOSMike 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 OpenShiftShekhar 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 2017Amazon 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 Wardmfrancis
 
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 MicroservicesDavid 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 ProvidersCisco 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 WebSphereXebiaLabs
 
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 iProximity Group
 
Real life-maf-2015
Real life-maf-2015Real life-maf-2015
Real life-maf-2015Luc 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-finalLuc 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 SandAdam 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 PinpointAmazon 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 2014Appear
 
JavaOne 2014: Java vs JavaScript
JavaOne 2014:   Java vs JavaScriptJavaOne 2014:   Java vs JavaScript
JavaOne 2014: Java vs JavaScriptChris Bailey
 

Similaire à 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
 

Dernier

Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Communityprachaibot
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.elesangwon
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxRomil Mishra
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESkarthi keyan
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxRomil Mishra
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
KCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitosKCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitosVictor Morales
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionSneha Padhiar
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodManicka Mamallan Andavar
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical trainingGladiatorsKasper
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfisabel213075
 

Dernier (20)

Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
2022 AWS DNA Hackathon 장애 대응 솔루션 jarvis.
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
 
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTESCME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
CME 397 - SURFACE ENGINEERING - UNIT 1 FULL NOTES
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Mine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptxMine Environment II Lab_MI10448MI__________.pptx
Mine Environment II Lab_MI10448MI__________.pptx
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
KCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitosKCD Costa Rica 2024 - Nephio para parvulitos
KCD Costa Rica 2024 - Nephio para parvulitos
 
Cost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based questionCost estimation approach: FP to COCOMO scenario based question
Cost estimation approach: FP to COCOMO scenario based question
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument method
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training
 
List of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdfList of Accredited Concrete Batching Plant.pdf
List of Accredited Concrete Batching Plant.pdf
 

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.