swift generics in theory and practice

55
Generics in Theory and Practice @MicheleTitolo

Upload: michele-titolo

Post on 22-Jan-2018

935 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Swift Generics in Theory and Practice

Generics in Theory and Practice

@MicheleTitolo

Page 2: Swift Generics in Theory and Practice

What We’ll Cover

Page 3: Swift Generics in Theory and Practice

What Is Generic Programming

Page 4: Swift Generics in Theory and Practice

Super Reusable

Page 5: Swift Generics in Theory and Practice

Type Independent

Page 6: Swift Generics in Theory and Practice

The Generics Manifesto

Page 7: Swift Generics in Theory and Practice

Not A Promise

Page 8: Swift Generics in Theory and Practice

Parts Implemented In Swift 3

Page 9: Swift Generics in Theory and Practice

Generics In The Standard Library

Page 10: Swift Generics in Theory and Practice

Collections

Page 11: Swift Generics in Theory and Practice

Can Hold Anything

Page 12: Swift Generics in Theory and Practice

Force Type Decisions

Page 13: Swift Generics in Theory and Practice

Global Functions

Page 14: Swift Generics in Theory and Practice

Widely Applicable

Page 15: Swift Generics in Theory and Practice

Transformations

Page 16: Swift Generics in Theory and Practice

Single Type Constraint If Any

Page 17: Swift Generics in Theory and Practice

Language Limitations

Page 18: Swift Generics in Theory and Practice

Variables That Are Classes + Protocols

Page 19: Swift Generics in Theory and Practice

Protocols With Associated Types

Page 20: Swift Generics in Theory and Practice

The stdlib Is Protocol Oriented

Page 21: Swift Generics in Theory and Practice

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)

Page 22: Swift Generics in Theory and Practice

One-Offs

Page 23: Swift Generics in Theory and Practice
Page 24: Swift Generics in Theory and Practice

Except When Working Around Language Limitations

Page 25: Swift Generics in Theory and Practice

Being Cool

Page 26: Swift Generics in Theory and Practice
Page 27: Swift Generics in Theory and Practice

What Can We Learn?

Page 28: Swift Generics in Theory and Practice

Generics Have Purpose

Page 29: Swift Generics in Theory and Practice

Design Decision Or Last Resort

Page 30: Swift Generics in Theory and Practice

Create True Reusability

Page 31: Swift Generics in Theory and Practice

Does Type Matter?

Page 32: Swift Generics in Theory and Practice

If It Matters, Probably Not Generic

Page 33: Swift Generics in Theory and Practice

Surface And Enforce Type Decisions

Page 34: Swift Generics in Theory and Practice

Using Generics Are Making Type Decisions

Page 35: Swift Generics in Theory and Practice

Generics Enforce That Choice

Page 36: Swift Generics in Theory and Practice

Any Is Poor Form

Page 37: Swift Generics in Theory and Practice

Coming Soon

Page 38: Swift Generics in Theory and Practice

Recursive Protocol Constraints

Page 39: Swift Generics in Theory and Practice

Associatedtypes Reference Enclosing Type

Page 40: Swift Generics in Theory and Practice

Sequence

Page 41: Swift Generics in Theory and Practice

And Leaky Abstractions

Page 42: Swift Generics in Theory and Practice

Nested Generics

Page 43: Swift Generics in Theory and Practice

Compiler Feature

Page 44: Swift Generics in Theory and Practice

Generics Can Contain Generics

Page 45: Swift Generics in Theory and Practice

Enhanced Existentials

Page 46: Swift Generics in Theory and Practice

Generic Types Are Enforced At Compile Time

Page 47: Swift Generics in Theory and Practice

Existential Types Are Enforced At Runtime

Page 48: Swift Generics in Theory and Practice

id<UITableViewDataSource, UITableViewDelegate>

Page 49: Swift Generics in Theory and Practice

UITableViewDataSource & UITableViewDelegate

Page 50: Swift Generics in Theory and Practice

Only Works With Concrete Protocols In Swift 3

Page 51: Swift Generics in Theory and Practice

Workaround With Generics

Page 52: Swift Generics in Theory and Practice

Goal To Be Fixed In Swift 4

Page 53: Swift Generics in Theory and Practice

Swift Is Evolving!

Page 54: Swift Generics in Theory and Practice

Get Involved

Page 55: Swift Generics in Theory and Practice

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/