Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
What I Learned From 55* Swift Standard Library Protocols
Greg Heo
October 29, 2015
Technology
6
1.2k
What I Learned From 55* Swift Standard Library Protocols
Greg Heo
October 29, 2015
Tweet
Share
More Decks by Greg Heo
See All by Greg Heo
gregheo
0
65
gregheo
0
45
gregheo
0
150
gregheo
0
16
gregheo
0
28
gregheo
0
490
gregheo
0
62
gregheo
0
34
gregheo
0
96
Other Decks in Technology
See All in Technology
muras
0
120
stakaya
14
8.3k
terurou
0
180
shoken
0
110
yaegashi
2
680
hirosys
0
150
qryuu
0
220
nozomiito
0
130
ymas0315
0
180
clustervr
0
170
redhatopenshift
0
530
pinboro
0
2.3k
Featured
See All Featured
orderedlist
PRO
329
35k
sferik
610
54k
garrettdimon
287
110k
morganepeng
17
1.1k
gr2m
83
11k
davidbonilla
70
3.5k
morganepeng
92
14k
philhawksworth
192
8.8k
vanstee
116
4.8k
cherdarchuk
71
260k
lara
590
61k
cassininazir
347
20k
Transcript
What I Learned From 55* Swift Standard Library Protocols by
@gregheo
@gregheo 55* Protocols 18 Minutes
@gregheo Why Protocols?
@gregheo Multiple Inheritance
@gregheo Animal Feline Canine Doge Pug Liger Jagwyre
@gregheo Animal Two Legs Canine Doge Pug Noisy Four Legs
Not Noisy etc. etc.
@gregheo
@gregheo 1. Protocol all the things 2. Open source it
3. ??? 4. Profit! ⭐
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType 1. Can do 2. Is a 3. Can be
@gregheo
@gregheo -able
@gregheo Hashable your instance → Int
@gregheo Equatable Comparable == != > < >= <=
@gregheo AbsoluteValuable public static func abs(x: Self) -> Self
@gregheo RawRepresentable instance ↔ raw value
@gregheo CustomPlaygroundQuickLookable instance → quick look view
@gregheo Operations Alternate Views So what?
@gregheo Image Filterable filtered image Video filtered video Audio filtered
audio
@gregheo protocol Thumbnailable { func thumbnailRepresentation() -> Self }
@gregheo Filterable Thumbnailable ↳ Can have a filter applied to
it ↳ Has an alternate thumbnail view
@gregheo Thing many many operations
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType 1. Can do 2. Is a 3. Can be
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType 1. Can do 2. Is a 3. Can be
@gregheo
@gregheo -Type
@gregheo CollectionType • Array • Dictionary • Set • Range
• String views
@gregheo IntegerType FloatingPointType BooleanType
@gregheo public protocol MirrorPathType // Do not declare new conformances
to this // protocol; they will not work as expected.
@gregheo ErrorType (To throw when handling errors)
@gregheo SequenceType GeneratorType (For iteration support)
@gregheo Protocols as Identity So what?
@gregheo Animal Four Legs Barks Mammal Canine
@gregheo Legs Two Legs Doge Liger Barks Four Legs Meows
Meme Friendly
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType 1. Can do 2. Is a 3. Can be
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType 1. Can do 2. Is a 3. Can be
@gregheo
@gregheo -Convertible
@gregheo FloatLiteralConvertible IntegerLiteralConvertible ArrayLiteralConvertible
@gregheo FloatLiteralConvertible init(floatLiteral value: FloatLiteralType) Float Your Type
@gregheo CustomStringConvertible String Your Type
@gregheo ExtendedGraphemeClusterLiteralConvertible
@gregheo Protocols for Convertibility So what?
@gregheo Contractor Employee Manager
@gregheo protocol EmployeeConvertible { func hire() -> Employee } class
Contractor: EmployeeConvertible { // ... } class Interviewee: EmployeeConvertible { // ... }
@gregheo Contractor Interviewee can be an Employee can be an
Employee
@gregheo Contractor Interviewee EmployeeConvertible can be an Employee
@gregheo protocol EmployeeConvertible { func hire() -> Employee } class
Contractor: EmployeeConvertible { // ... } class Interviewee: EmployeeConvertible { // ... }
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType 1. Can do 2. Is a 3. Can be
@gregheo AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType,
CollectionType, Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType 1. Can do 2. Is a 3. Can be
@gregheo 1. Operations 2. Alternate views 3. Identity 4. Conversion
@gregheo
Protocols FTW! @gregheo use them! say hello! wow