swift generics in theory and practice
TRANSCRIPT
Generics in Theory and Practice
@MicheleTitolo
What We’ll Cover
What Is Generic Programming
Super Reusable
Type Independent
The Generics Manifesto
Not A Promise
Parts Implemented In Swift 3
Generics In The Standard Library
Collections
Can Hold Anything
Force Type Decisions
Global Functions
Widely Applicable
Transformations
Single Type Constraint If Any
Language Limitations
Variables That Are Classes + Protocols
Protocols With Associated Types
The stdlib Is Protocol Oriented
public init<Subject, C : Collection where C.Iterator.Element == Child, C.SubSequence : Collection, C.SubSequence.Iterator.Element == Child, C.SubSequence.Index == C.Index, C.SubSequence.Indices : Collection, C.SubSequence.Indices.Iterator.Element == C.Index, C.SubSequence.Indices.Index == C.Index, C.SubSequence.Indices.SubSequence == C.SubSequence.Indices, C.SubSequence.SubSequence == C.SubSequence, C.Indices : Collection, C.Indices.Iterator.Element == C.Index, C.Indices.Index == C.Index, C.Indices.SubSequence == C.Indices>(_ subject: Subject, children: C, displayStyle: Mirror.DisplayStyle? = default, ancestorRepresentation: Mirror.AncestorRepresentation = default)
One-Offs
Except When Working Around Language Limitations
Being Cool
What Can We Learn?
Generics Have Purpose
Design Decision Or Last Resort
Create True Reusability
Does Type Matter?
If It Matters, Probably Not Generic
Surface And Enforce Type Decisions
Using Generics Are Making Type Decisions
Generics Enforce That Choice
Any Is Poor Form
Coming Soon
Recursive Protocol Constraints
Associatedtypes Reference Enclosing Type
Sequence
And Leaky Abstractions
Nested Generics
Compiler Feature
Generics Can Contain Generics
Enhanced Existentials
Generic Types Are Enforced At Compile Time
Existential Types Are Enforced At Runtime
id<UITableViewDataSource, UITableViewDelegate>
UITableViewDataSource & UITableViewDelegate
Only Works With Concrete Protocols In Swift 3
Workaround With Generics
Goal To Be Fixed In Swift 4
Swift Is Evolving!
Get Involved
Photo Credits•https://unsplash.com/search/birds?photo=XzW66yAyVBA•https://unsplash.com/search/birds?photo=GL6ORxDMswI•https://unsplash.com/search/birds?photo=kuJkUTxR0z4•https://unsplash.com/search/birds?photo=ea4OvouLpZk•https://unsplash.com/search/birds?photo=W341a11Rt1g•https://unsplash.com/search/birds?photo=eRxKUD3S7M8•https://unsplash.com/search/birds?photo=WooFf3yav6U•https://unsplash.com/search/birds?photo=yhnOTbw9SPY•https://www.flickr.com/photos/88776252@N00/5332505572/