What I Learned From 55* Swift Standard Library Protocols

06609d73ad2165c4aafcf65a1ddb9563?s=47 Greg Heo
October 29, 2015

What I Learned From 55* Swift Standard Library Protocols

06609d73ad2165c4aafcf65a1ddb9563?s=128

Greg Heo

October 29, 2015
Tweet

Transcript

  1. What I Learned From 55* Swift Standard Library Protocols by

    @gregheo
  2. @gregheo 55* Protocols 18 Minutes

  3. @gregheo Why Protocols?

  4. @gregheo Multiple Inheritance

  5. @gregheo Animal Feline Canine Doge Pug Liger Jagwyre

  6. @gregheo Animal Two Legs Canine Doge Pug Noisy Four Legs

    Not Noisy etc. etc.
  7. @gregheo

  8. @gregheo 1. Protocol all the things 2. Open source it

    3. ??? 4. Profit! ⭐
  9. @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
  10. @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
  11. @gregheo

  12. @gregheo -able

  13. @gregheo Hashable your instance → Int

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

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

  16. @gregheo RawRepresentable instance ↔ raw value

  17. @gregheo CustomPlaygroundQuickLookable instance → quick look view

  18. @gregheo Operations Alternate Views So what?

  19. @gregheo Image Filterable filtered image Video filtered video Audio filtered

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

  21. @gregheo Filterable Thumbnailable ↳ Can have a filter applied to

    it ↳ Has an alternate thumbnail view
  22. @gregheo Thing many many operations

  23. @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
  24. @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
  25. @gregheo

  26. @gregheo -Type

  27. @gregheo CollectionType • Array • Dictionary • Set • Range

    • String views
  28. @gregheo IntegerType FloatingPointType BooleanType

  29. @gregheo public protocol MirrorPathType // Do not declare new conformances

    to this // protocol; they will not work as expected.

  30. @gregheo ErrorType (To throw when handling errors)

  31. @gregheo SequenceType GeneratorType (For iteration support)

  32. @gregheo Protocols as Identity So what?

  33. @gregheo Animal Four Legs Barks Mammal Canine

  34. @gregheo Legs Two Legs Doge Liger Barks Four Legs Meows

    Meme Friendly
  35. @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
  36. @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
  37. @gregheo

  38. @gregheo -Convertible

  39. @gregheo FloatLiteralConvertible IntegerLiteralConvertible ArrayLiteralConvertible

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

  41. @gregheo CustomStringConvertible String Your Type

  42. @gregheo ExtendedGraphemeClusterLiteralConvertible

  43. @gregheo Protocols for Convertibility So what?

  44. @gregheo Contractor Employee Manager

  45. @gregheo protocol EmployeeConvertible { func hire() -> Employee } class

    Contractor: EmployeeConvertible { // ... } class Interviewee: EmployeeConvertible { // ... }
  46. @gregheo Contractor Interviewee can be an Employee can be an

    Employee
  47. @gregheo Contractor Interviewee EmployeeConvertible can be an Employee

  48. @gregheo protocol EmployeeConvertible { func hire() -> Employee } class

    Contractor: EmployeeConvertible { // ... } class Interviewee: EmployeeConvertible { // ... }
  49. @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
  50. @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
  51. @gregheo 1. Operations 2. Alternate views 3. Identity 4. Conversion

  52. @gregheo

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