Slide 1

Slide 1 text

What I Learned From 55* Swift Standard Library Protocols by @gregheo

Slide 2

Slide 2 text

@gregheo 55* Protocols 18 Minutes

Slide 3

Slide 3 text

@gregheo Why Protocols?

Slide 4

Slide 4 text

@gregheo Multiple Inheritance

Slide 5

Slide 5 text

@gregheo Animal Feline Canine Doge Pug Liger Jagwyre

Slide 6

Slide 6 text

@gregheo Animal Two Legs Canine Doge Pug Noisy Four Legs Not Noisy etc. etc.

Slide 7

Slide 7 text

@gregheo

Slide 8

Slide 8 text

@gregheo 1. Protocol all the things 2. Open source it 3. ??? 4. Profit! ⭐

Slide 9

Slide 9 text

@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

Slide 10

Slide 10 text

@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

Slide 11

Slide 11 text

@gregheo

Slide 12

Slide 12 text

@gregheo -able

Slide 13

Slide 13 text

@gregheo Hashable your instance → Int

Slide 14

Slide 14 text

@gregheo Equatable Comparable == != > < >= <=

Slide 15

Slide 15 text

@gregheo AbsoluteValuable public static func abs(x: Self) -> Self

Slide 16

Slide 16 text

@gregheo RawRepresentable instance ↔ raw value

Slide 17

Slide 17 text

@gregheo CustomPlaygroundQuickLookable instance → quick look view

Slide 18

Slide 18 text

@gregheo Operations Alternate Views So what?

Slide 19

Slide 19 text

@gregheo Image Filterable filtered image Video filtered video Audio filtered audio

Slide 20

Slide 20 text

@gregheo protocol Thumbnailable { func thumbnailRepresentation() -> Self }

Slide 21

Slide 21 text

@gregheo Filterable Thumbnailable ↳ Can have a filter applied to it ↳ Has an alternate thumbnail view

Slide 22

Slide 22 text

@gregheo Thing many many operations

Slide 23

Slide 23 text

@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

Slide 24

Slide 24 text

@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

Slide 25

Slide 25 text

@gregheo

Slide 26

Slide 26 text

@gregheo -Type

Slide 27

Slide 27 text

@gregheo CollectionType • Array • Dictionary • Set • Range • String views

Slide 28

Slide 28 text

@gregheo IntegerType FloatingPointType BooleanType

Slide 29

Slide 29 text

@gregheo public protocol MirrorPathType // Do not declare new conformances to this // protocol; they will not work as expected.


Slide 30

Slide 30 text

@gregheo ErrorType (To throw when handling errors)

Slide 31

Slide 31 text

@gregheo SequenceType GeneratorType (For iteration support)

Slide 32

Slide 32 text

@gregheo Protocols as Identity So what?

Slide 33

Slide 33 text

@gregheo Animal Four Legs Barks Mammal Canine

Slide 34

Slide 34 text

@gregheo Legs Two Legs Doge Liger Barks Four Legs Meows Meme Friendly

Slide 35

Slide 35 text

@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

Slide 36

Slide 36 text

@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

Slide 37

Slide 37 text

@gregheo

Slide 38

Slide 38 text

@gregheo -Convertible

Slide 39

Slide 39 text

@gregheo FloatLiteralConvertible IntegerLiteralConvertible ArrayLiteralConvertible

Slide 40

Slide 40 text

@gregheo FloatLiteralConvertible init(floatLiteral value: FloatLiteralType) Float Your Type

Slide 41

Slide 41 text

@gregheo CustomStringConvertible String Your Type

Slide 42

Slide 42 text

@gregheo ExtendedGraphemeClusterLiteralConvertible

Slide 43

Slide 43 text

@gregheo Protocols for Convertibility So what?

Slide 44

Slide 44 text

@gregheo Contractor Employee Manager

Slide 45

Slide 45 text

@gregheo protocol EmployeeConvertible { func hire() -> Employee } class Contractor: EmployeeConvertible { // ... } class Interviewee: EmployeeConvertible { // ... }

Slide 46

Slide 46 text

@gregheo Contractor Interviewee can be an Employee can be an Employee

Slide 47

Slide 47 text

@gregheo Contractor Interviewee EmployeeConvertible can be an Employee

Slide 48

Slide 48 text

@gregheo protocol EmployeeConvertible { func hire() -> Employee } class Contractor: EmployeeConvertible { // ... } class Interviewee: EmployeeConvertible { // ... }

Slide 49

Slide 49 text

@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

Slide 50

Slide 50 text

@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

Slide 51

Slide 51 text

@gregheo 1. Operations 2. Alternate views 3. Identity 4. Conversion

Slide 52

Slide 52 text

@gregheo

Slide 53

Slide 53 text

Protocols FTW! @gregheo use them! say hello! wow