Slide 1

Slide 1 text

Protocol and Value
 Oriented Programming A new programming paradigm with Swift? @jerry.ko

Slide 2

Slide 2 text

What is Protocol?

Slide 3

Slide 3 text

A protocol defines a blueprint of requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class to provide an actual implementation of those requirements. You can inherit a protocol to build a hierarchy of protocols and to add more requirements.

Slide 4

Slide 4 text

protocol == interface?

Slide 5

Slide 5 text

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. You can inherit a protocol to build a hierarchy of protocols and to add more requirements. In addition, you can extend a protocol to implement some of these requirements.

Slide 6

Slide 6 text

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. You can inherit a protocol to build a hierarchy of protocols and to add more requirements. In addition, you can extend a protocol to implement some of these requirements.

Slide 7

Slide 7 text

Property/method requirements for class/struct/enum protocol FullyNamed { var fullName: String { get } func printFullName() } class Cat: FullyNamed { var fullName: String func printFullName() { print(“A cat named \(fullName)") } } struct Person: FullyNamed { var fullName: String func printFullName() { print(“A person named \(fullName)") } } protocol Togglable { mutating func toggle() } enum OnOffSwitch: Togglable { case off, on mutating func toggle() { switch self { case .off: self = .on case .on: self = .off } } }

Slide 8

Slide 8 text

Protocol with Extension struct Car { let year: Int let modelName: String ... } extension Car: FullyNamed { var fullName: String { return "\(year) \(modelName)" } } Adopting Protocol Retroactively protocol FullyNamed { var fullName: String { get } func printFullName() } extension FullyNamed { func printFullName() { print("It's name is \(fullName)") } } Providing Default Implementation

Slide 9

Slide 9 text

Conditional Protocol Adoption extension Array: TextRepresentable where Element: TextRepresentable { var textualDescription: String { let itemsAsText = self.map { $0.textualDescription } return "[" + itemsAsText.joined(separator: ", ") + "]" } }

Slide 10

Slide 10 text

What’s Wrong with Inheritance?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Inheritance, OOP One superclass — choose well! No retroactive modeling Superclass may have stored properties • You must accept them • Initialization burden Should know what/how to override (and when not to)

Slide 13

Slide 13 text

Protocol in Action

Slide 14

Slide 14 text

class Ordered { func precedes(other: Ordered) -> Bool { fatalError("implement me!") } } class Number: Ordered { var value: Double = 0 override func precedes(other: Ordered) -> Bool { return value < (other as! Number).value } }

Slide 15

Slide 15 text

class Ordered { func precedes(other: Ordered) -> Bool { fatalError("implement me!") } } class Number: Ordered { var value: Double = 0 override func precedes(other: Ordered) -> Bool { return value < (other as! Number).value } }

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

protocol Ordered { func precedes(other: Self) -> Bool } class Number: Ordered, Printable, ... { var value: Double = 0 func precedes(other: Number) -> Bool { return self.value < other.value } }

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

protocol ViewWithBorder { var borderColor: UIColor { get } var borderThickness: CGFloat { get } init(borderColor: UIColor, borderThickness: CGFloat, frame: CGRect) } extension ViewWithBorder where Self: UIView { func addBorder() -> Void { layer.borderColor = borderColor.cgColor layer.borderWidth = borderThickness } } extension UIButton: ViewWithBorder {} extension UIImageView: ViewWithBorder {} button.addBorder() imageView.addBorder()

Slide 20

Slide 20 text

Don’t start with a class. 
 Start with a protocol!

Slide 21

Slide 21 text

Reference vs. Value

Slide 22

Slide 22 text

Problems with Reference ?

Slide 23

Slide 23 text

Problems with Reference Unintended Sharing

Slide 24

Slide 24 text

Implicit Sharing, the Root of Bugs! Mutation Race Condition Locks Complexity Ineffiency

Slide 25

Slide 25 text

How to deal with it?

Slide 26

Slide 26 text

Manual Copying

Slide 27

Slide 27 text

Defensive Copying

Slide 28

Slide 28 text

Eliminating Mutation

Slide 29

Slide 29 text

Eliminating Mutation

Slide 30

Slide 30 text

Value Semantics

Slide 31

Slide 31 text

Variables are Logically Distinct Value Types are Distinguished by Value

Slide 32

Slide 32 text

Values are Copied. Performance with all the copies?

Slide 33

Slide 33 text

Copies are Cheap

Slide 34

Slide 34 text

In Swift,

Slide 35

Slide 35 text

Value Type in Action

Slide 36

Slide 36 text

Implementing Undo

Slide 37

Slide 37 text

Implementing Undo

Slide 38

Slide 38 text

Implementing Undo

Slide 39

Slide 39 text

class? struct? Then, struct!

Slide 40

Slide 40 text

So, POP with Value Semantics is 
 the Future of Programming Language?

Slide 41

Slide 41 text

Composition over Inheritance?

Slide 42

Slide 42 text

Mixing Value Types and Reference Types Maintaining value semantics requires special considerations! • How do we cope with mutation of the referenced object? • How does the reference identity affect equality? Unrestricted mutation of referenced objects breaks value semantics. • Non-mutating operations are always safe • Mutating operations must first copy

Slide 43

Slide 43 text

However,

Slide 44

Slide 44 text

TIOBE Index for October 2018

Slide 45

Slide 45 text

Swift is the latest language that is already popular and backed up by giant brother, Apple. So, it’s worth having POP and Value Semantics in mind!

Slide 46

Slide 46 text

Thank you