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
66
gregheo
0
45
gregheo
0
160
gregheo
0
16
gregheo
0
28
gregheo
0
490
gregheo
0
63
gregheo
0
34
gregheo
0
97
Other Decks in Technology
See All in Technology
1027kg
0
200
sat
40
29k
masakazu
0
170
simosako
1
130
mizzy
1
110
imdigitallab
0
340
pohjus
0
3.3k
gobeyond20xx
0
210
twada
PRO
6
2k
mmarukaw
0
1.9k
line_developers
PRO
0
180
thockin
3
920
Featured
See All Featured
jakevdp
775
200k
cassininazir
347
20k
erikaheidi
14
4.3k
edds
56
9.4k
andyhume
63
3.7k
imathis
479
150k
destraynor
222
47k
dougneiner
55
5.4k
reverentgeek
27
2k
nonsquared
81
3.4k
akmur
252
19k
lara
590
61k
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