Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Speaking in Types
Search
TJ Usiyan
October 16, 2015
Programming
0
28
Speaking in Types
Slides for 2015 October 16 talk at CocoaConf
TJ Usiyan
October 16, 2015
Tweet
Share
More Decks by TJ Usiyan
See All by TJ Usiyan
Musical Phantoms
griotspeak
0
68
Property-Based Testing with SwiftCheck
griotspeak
1
5.5k
Other Decks in Programming
See All in Programming
Build Apps for iOS, Android & Desktop in 100% Kotlin With Compose Multiplatform (mDevCamp 2024)
zsmb
0
330
『Railsオワコン』と言われる時代に、なぜブルーモ証券はRailsを選ぶのか
free_world21
0
210
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
TCAとKMPを用いた新規動画配信アプリ 「ABEMA Live」の設計
tomu28
1
100
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
ONE WEDGE_company_guide
1wedge_one
0
480
StoreKit2によるiOSのアプリ内課金のリニューアル
kangnux
0
110
ADRを一年運用してみた/adr_after_a_year
hanhan1978
7
2.4k
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
370
VSCodeでのDatabricks開発もお勧めしたい/I would also recommend Databricks development with VSCode.
kazumain
0
250
2 週間で Twitter Bot を作ってみた
contour_gara
0
380
Goのエラースタックトレースの歴史と今後
sonatard
8
1.4k
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
16
2.1k
Building a Scalable Design System with Sketch
lauravandoore
456
32k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
21
1.6k
Writing Fast Ruby
sferik
621
60k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
StorybookのUI Testing Handbookを読んだ
zakiyama
13
4.6k
Designing on Purpose - Digital PM Summit 2013
jponch
110
6.5k
Code Review Best Practice
trishagee
55
15k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
244
20k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
40
4.4k
Into the Great Unknown - MozCon
thekraken
10
990
Agile that works and the tools we love
rasmusluckow
325
20k
Transcript
Speaking in Types How I Learned to Stop Worrying and
Love the Type System TJ Usiyan - @griotspeak © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 1
How can we examine types? © Speaking in Types -
CocoaConf San Jose - @griotspeak, 2015 2
Examining Types in Xcode • Option click • Look at
declarations • Look at the generated header © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 3
What is a Type? © Speaking in Types - CocoaConf
San Jose - @griotspeak, 2015 4
What is a Type? The description of a portion of
our system's domain. © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 5
What is a Type? Types are how we describe the
data that our system works with. © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 6
In Objective-C, Compiler puts trust in you. In Swift, you
put trust in compiler — Yakko Smirnoff I'm Sorry Dave. I'm Afraid I can't do that. © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 7
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 8
NSError NSError *error = nil; [myContext save:&error]; if (!error) {
/* "Woohoo" */ } else { /* "Oh noes!" */ } © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 9
NSError Redux NSError *error = nil; BOOL success = [myContext
save:&error]; if (success) { /* "Woohoo" */ } else { /* "Oh noes!" */ } © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 10
NSError Re-Redux do { try/*(?|!)?*/ myContext.save() } catch { }
© Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 11
Result enum Result<T> { case Error(NSError) case Success(T) } ©
Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 12
"An Either by any other name would compile as sweet."
— Gilliam Shakespeare enum Either<T, U> { case Left(T) case Right(U) } © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 13
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 14
Make Illegal States Unrepresentable — Yaron Minsky let contactInfo:(PhoneNumber?, EmailAddress?)
© Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 15
enum ContactInfo { case Phone(PhoneNumber) case Email(EmailAddress) case PhoneAndEmail(PhoneNumber, EmailAddress)
} © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 16
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 17
On Semantics Demo © Speaking in Types - CocoaConf San
Jose - @griotspeak, 2015 18
Enums Are Delightful. enum FileLoadingResult { case Success(NSData) case LoadError(NSError)
} © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 19
Enums Are Delightful. enum Foo {} © Speaking in Types
- CocoaConf San Jose - @griotspeak, 2015 20
Enums Are Delightful. enum MyNamespace {} © Speaking in Types
- CocoaConf San Jose - @griotspeak, 2015 21
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 22
Optionals Are Quite Nice as Well. typedef NS_ENUM(NSInteger, YouCanGetWith) {
YouCanGetWithNull = 0, YouCanGetWithThis, YouCanGetWithThat}; © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 23
Optionals Are Quite Nice as Well. typedef NS_ENUM(NSInteger, YouCanGetWith) {
YouCanGetWithNull = 0, YouCanGetWithThis, YouCanGetWithThat}; vs enum YouCanGetWith { case This, That } © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 24
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 25
Putting the 'Algebra' in Algebraic Data Types © Speaking in
Types - CocoaConf San Jose - @griotspeak, 2015 26
Algebra of Data Types - Zero Type Void /// The
empty tuple type. /// /// This is the default return type of /// functions for which no explicit /// return type is specified. typealias Void = () © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 27
Algebra of Data Types - Sum Types Enums enum PrimaryColor
{ case Red, Blue, Yellow } enum Either<T, U> { case Left(T) case Right(U) } let foo:Either<Bool, PrimaryColor> // 2(Bool) + 3(PrimaryColor) possible values © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 28
Algebra of Data Types - Product Types Tuples let bar:(Bool,
PrimaryColor) // 2(Bool) * 3(PrimaryColor) possible values © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 29
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 30
Functions Types Need Love Too. — Samantha Wolf © Speaking
in Types - CocoaConf San Jose - @griotspeak, 2015 31
Functions are Maps! func myRandom() -> Int func consumeNumber(Int) //
(Int) -> () func sortNums([Int]) -> [Int] © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 32
Function Types func find<C : CollectionType where C.Generator.Element : Equatable>
(domain: C, value: C.Generator.Element) -> [C.Generator.Element] vs func find<C : CollectionType where C.Generator.Element : Equatable> (domain: C, value: C.Generator.Element) -> C.Index? vs func find<C : CollectionType where C.Generator.Element : Equatable> (domain: C, value: C.Generator.Element) -> C.Generator.Element © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 33
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 34
Optional Initializers …when illegal state is completely worth it struct
ScaleDegree { init(diatonicValue:DiatonicValue, chromaticValue:ChromaticValue) init(accidental:Accidental, diatonicValue:DiatonicValue) init?(quality:Interval.Quality, diatonic:DiatonicValue) } let scaleDegree = ScaleDegree(.Perfect, .Fifth)! © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 35
Polymorphism! © Speaking in Types - CocoaConf San Jose -
@griotspeak, 2015 36
Parametric Polymorphism struct Array<T> class Thing<T> func foo<T> protocol BazableType
{ typealias T } © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 37
Parametric Polymorphism • Structs • Enums • Classes • Prototypes
• Structs • Classes © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 38
Trait Polymorphism struct Array<T:Equatable> class Thing<T:Equatable> func foo<T:Equatable> © Speaking
in Types - CocoaConf San Jose - @griotspeak, 2015 39
Trait Polymorphism • Structs • Enums • Classes • Functions
© Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 40
© Speaking in Types - CocoaConf San Jose - @griotspeak,
2015 41
Generic Function Signatures <T>(T) -> () © Speaking in Types
- CocoaConf San Jose - @griotspeak, 2015 42
Generic Function Signatures <T>([T]) -> [T] © Speaking in Types
- CocoaConf San Jose - @griotspeak, 2015 43
Generic Function Signatures <T: Comparable>([T]) -> [T] © Speaking in
Types - CocoaConf San Jose - @griotspeak, 2015 44
Generic Function Signatures <T, U>(T) -> U © Speaking in
Types - CocoaConf San Jose - @griotspeak, 2015 45
Generic Function Signatures <T>(T, T) -> T © Speaking in
Types - CocoaConf San Jose - @griotspeak, 2015 46
Generic Function Signatures <T,U,V>((U -> V), (T -> U)) ->
(T -> V) © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 47
Names Are Part of a Type func stride<T : Strideable>(from
start: T, to end: T, by stride: T.Stride) -> StrideTo<T> func stride<T : Strideable>(from start: T, through end: T, by stride: T.Stride) -> StrideThrough<T> © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 48
Names Are Part of a Type … start: T, to
end: … … start: T, through end: … © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 49
Function Overloading © Speaking in Types - CocoaConf San Jose
- @griotspeak, 2015 50
Function Overloading +(T, T) -> T © Speaking in Types
- CocoaConf San Jose - @griotspeak, 2015 51
Function Overloading func +(lhs: Float, rhs: Float) -> Float func
+<T>(lhs: Int, rhs: UnsafePointer<T>) -> UnsafePointer<T> func +<T>(lhs: UnsafePointer<T>, rhs: Int) -> UnsafePointer<T> func +(lhs: Int, rhs: Int) -> Int func +(lhs: UInt, rhs: UInt) -> UInt func +(lhs: Int64, rhs: Int64) -> Int64 func +(lhs: UInt64, rhs: UInt64) -> UInt64 func +<T>(lhs: Int, rhs: UnsafeMutablePointer<T>) -> UnsafeMutablePointer<T> func +<T>(lhs: UnsafeMutablePointer<T>, rhs: Int) -> UnsafeMutablePointer<T> func +(lhs: Int32, rhs: Int32) -> Int32 func +(lhs: UInt32, rhs: UInt32) -> UInt32 func +(lhs: Int16, rhs: Int16) -> Int16 func +(lhs: UInt16, rhs: UInt16) -> UInt16 func +(lhs: Int8, rhs: Int8) -> Int8 func +(lhs: UInt8, rhs: UInt8) -> UInt8 func +(lhs: Double, rhs: Double) -> Double func +(lhs: String, rhs: String) -> String // `SummableType` anyone? © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 52
Higher Kinded Types © Speaking in Types - CocoaConf San
Jose - @griotspeak, 2015 53
Higher Kinded Types func ==<T : Equatable>(lhs: T?, rhs: T?)
-> Bool /// Returns true if these arrays contain the same elements. func ==<T : Equatable>(lhs: [T], rhs: [T]) -> Bool /// Returns true if these arrays contain the same elements. func ==<T : Equatable>(lhs: Slice<T>, rhs: Slice<T>) -> Bool /// Returns true if these arrays contain the same elements. func ==<T : Equatable>(lhs: ContiguousArray<T>, rhs: ContiguousArray<T>) -> Bool © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 54
Higher Kinded Types How can we state that a container
holding an Equatable type is Equatable? © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 55
Higher Kinded Types How can we state that a container
holding an Equatable type is Equatable? © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 56
Higher Kinded Types We must repeat ourselves. This fact is
unfortunate. © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 57
Higher Kinded Types - Map, Filter, and Friends /// Return
an `Array` containing the results of mapping `transform` /// over `source`. func map<S : SequenceType, T>(source: S, transform: (S.Generator.Element) -> T) -> [T] /// Return an `Array` containing the results of mapping `transform` /// over `source`. func map<C : CollectionType, T>(source: C, transform: (C.Generator.Element) -> T) -> [T] © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 58
• The type system is our friend • Types can
hold more meaning than in Obj-C. © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 59
Sources • Effective ML 1 • Algebra of Data Types
[^2] 1 https://vimeo.com/14313378 [^2]:http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-types/ © Speaking in Types - CocoaConf San Jose - @griotspeak, 2015 60