building better apps with value types in swift€¦ · value types in swift doug gregor language...
TRANSCRIPT
![Page 1: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/1.jpg)
© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
Building Better Apps withValue Types in Swift
Doug Gregor Language LawyerBill Dudney Arranger of Bits
Developer Tools #WWDC15
Session 414
![Page 2: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/2.jpg)
Roadmap
Reference semanticsImmutabilityValue semanticsValue types in practiceMixing value types and reference types
![Page 3: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/3.jpg)
Reference Semantics
![Page 4: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/4.jpg)
A Temperature Class
class Temperature { var celsius: Double = 0 var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } }
![Page 5: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/5.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
Using Our Temperature Class
![Page 6: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/6.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
Using Our Temperature Class
![Page 7: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/7.jpg)
Why Is It So Hot in Here?
![Page 8: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/8.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75
Unintended Sharing
Housethermostat
oven
Thermostattemperature
Oventemperature
Temperaturecelsius: 23.88
temp
![Page 9: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/9.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
Unintended Sharing
Housethermostat
oven
Thermostattemperature
Oventemperature
Temperaturecelsius: 23.88
temp
![Page 10: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/10.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425
Unintended Sharing
Housethermostat
oven
Thermostattemperature
Oventemperature
Temperaturecelsius: 218.3
temp
![Page 11: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/11.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
Unintended Sharing
Housethermostat
oven
Thermostattemperature
Oventemperature
Temperaturecelsius: 218.3
temp
![Page 12: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/12.jpg)
Copy When You Need It
![Page 13: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/13.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75
Manual Copying
Housethermostat
oven
Thermostattemperature
Oventemperature
Temperaturecelsius: 23.88
temp
![Page 14: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/14.jpg)
Temperaturecelsius: 23.88Temperaturecelsius: 23.88
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp.copy()
Manual Copying
Housethermostat
oven
Thermostattemperature
Oventemperature
temp
![Page 15: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/15.jpg)
Temperaturecelsius: 23.88
Temperaturecelsius: 23.88
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp.copy()
Manual Copying
Housethermostat
oven
Thermostattemperature
Oventemperature
temp
![Page 16: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/16.jpg)
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp.copy()
temp.fahrenheit = 425
Manual Copying
Housethermostat
oven
Thermostattemperature
Oventemperature
Temperaturecelsius: 218.3
temp
Temperaturecelsius: 23.88
![Page 17: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/17.jpg)
Temperaturecelsius: 218.3Temperaturecelsius: 218.3
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp.copy()
temp.fahrenheit = 425 home.oven.temperature = temp.copy() home.oven.bake()
Manual Copying
Housethermostat
oven
Thermostattemperature
Oventemperature
temp
Temperaturecelsius: 23.88
![Page 18: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/18.jpg)
Temperaturecelsius: 218.3
Temperaturecelsius: 218.3
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp.copy()
temp.fahrenheit = 425 home.oven.temperature = temp.copy() home.oven.bake()
Manual Copying
Housethermostat
oven
Thermostattemperature
Oventemperature
temp
Temperaturecelsius: 23.88
![Page 19: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/19.jpg)
Defensive Copying
class Oven { var _temperature: Temperature = Temperature(celsius: 0)
var temperature: Temperature { get { return _temperature } set { _temperature = newValue.copy() } } }
![Page 20: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/20.jpg)
Defensive Copying
class Thermostat { var _temperature: Temperature = Temperature(celsius: 0)
var temperature: Temperature { get { return _temperature } set { _temperature = newValue.copy() } } }
![Page 21: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/21.jpg)
Copying in Cocoa[Touch] and Objective-C
Cocoa[Touch] requires copying throughout• NSCopying codifies copying an object• NSString, NSArray, NSDictionary, NSURLRequest, etc. all require copying
![Page 22: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/22.jpg)
Defensive Copying in Cocoa and Objective-C
Cocoa[Touch] requires copying throughout• NSCopying codifies copying an object• NSString, NSArray, NSDictionary, NSURLRequest, etc. all require copying
Defensive copying pervades Cocoa[Touch] and Objective-C• NSDictionary calls -copy on its keys• Property copy attribute provides defensive copying on assignment
![Page 23: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/23.jpg)
Defensive Copying in Cocoa and Objective-C
Cocoa[Touch] requires copying throughout• NSCopying codifies copying an object• NSString, NSArray, NSDictionary, NSURLRequest, etc. all require copying
Defensive copying pervades Cocoa[Touch] and Objective-C• NSDictionary calls -copy on its keys• Property copy attribute provides defensive copying on assignment It’s still not enough…bugs abound due to missed copies
![Page 24: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/24.jpg)
Is Immutability the Answer?
![Page 25: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/25.jpg)
Eliminating Mutation
Functional programming languages have reference semantics with immutabilityEliminates many problems caused by reference semantics with mutation• No worries about unintended side effects
![Page 26: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/26.jpg)
Eliminating Mutation
Functional programming languages have reference semantics with immutabilityEliminates many problems caused by reference semantics with mutation• No worries about unintended side effects
Several notable disadvantages• Can lead to awkward interfaces• Does not map efficiently to the machine model
![Page 27: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/27.jpg)
An Immutable Temperature Class
class Temperature { let celsius: Double = 0 var fahrenheit: Double { return celsius * 9 / 5 + 32 }
init(celsius: Double) { self.celsius = celsius } init(fahrenheit: Double) { self.celsius = (fahrenheit - 32) * 5 / 9 } }
![Page 28: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/28.jpg)
Awkward Immutable Interfaces
With mutabilityhome.oven.temperature.fahrenheit += 10.0
![Page 29: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/29.jpg)
With mutabilityhome.oven.temperature.fahrenheit += 10.0
Without mutabilitylet temp = home.oven.temperature home.oven.temperature = Temperature(fahrenheit: temp.fahrenheit + 10.0)
Awkward Immutable Interfaces
![Page 30: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/30.jpg)
With mutabilityhome.oven.temperature.fahrenheit += 10.0
Without mutabilitylet temp = home.oven.temperature home.oven.temperature = Temperature(fahrenheit: temp.fahrenheit + 10.0)
Awkward Immutable Interfaces
![Page 31: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/31.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
![Page 32: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/32.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
![Page 33: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/33.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
2018 1916 1714 1512 1310 118 96 74 52 3
![Page 34: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/34.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
2018 1916 1714 1512 1310 118 96 74 52 3
![Page 35: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/35.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
2018 1916 1714 1512 1310 118 96 74 52 3
![Page 36: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/36.jpg)
201816141210864 19171513119752 3
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
![Page 37: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/37.jpg)
19171513119752 3
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
0 0 0 0 0 0 0 0 0
![Page 38: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/38.jpg)
19171513119752 3
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
0 0 0 0 0 0 0 0 0
![Page 39: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/39.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
000000000 19171513119752 3
![Page 40: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/40.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
000000000 19171311752 3 0 0
![Page 41: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/41.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
000000 1917013110752 3 0 00
![Page 42: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/42.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
000000 1917013110752 3 0 00
![Page 43: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/43.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
0000000 191713110752 32 3 5 117 13 17 190 00
![Page 44: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/44.jpg)
Sieve of Eratosthenes
func primes(n: Int) -> [Int] { var numbers = [Int](2..<n) for i in 0..<n-2 { guard let prime = numbers[i] where prime > 0 else { continue } for multiple in stride(from: 2 * prime-2, to: n-2, by: prime) { numbers[multiple] = 0 } } return numbers.filter { $0 > 0 } }
0000000 191713110
7
52 3
2 3 5 11
7
13 17 19
0 00
![Page 45: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/45.jpg)
Functional Sieve of Eratosthenes
Haskell:primes = sieve [2..]sieve [] = []sieve (p : xs) = p : sieve [x | x <− xs, x ‘mod‘ p > 0]
![Page 46: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/46.jpg)
Functional Sieve of Eratosthenes
Haskell:primes = sieve [2..]sieve [] = []sieve (p : xs) = p : sieve [x | x <− xs, x ‘mod‘ p > 0]
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
![Page 47: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/47.jpg)
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
Functional Sieve of Eratosthenes
2 2018 1916 1714 1512 1310 118 96 74 53
![Page 48: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/48.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 53
![Page 49: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/49.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 53
![Page 50: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/50.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 533 5 7 9 11 13 15 17 19
![Page 51: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/51.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 53
3 5 7 9 11 13 15 17 19
![Page 52: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/52.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 53
3 5 7 9 11 13 15 17 19
![Page 53: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/53.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 53
3 5 7 9 11 13 15 17 19
![Page 54: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/54.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 53
3 5 7 9 11 13 15 17 195 7 11 13 17 19
![Page 55: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/55.jpg)
Functional Sieve of Eratosthenes
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
2 2018 1916 1714 1512 1310 118 96 74 53
3 5 7 9 11 13 15 17 19
5 7 11 13 17 19
![Page 56: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/56.jpg)
Functional Sieve
2 2018 1916 1714 1512 1310 118 96 74 53
3 5 7 9 11 13 15 17 19
5 7 11 13 17 19
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
of Eratosthenes
![Page 57: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/57.jpg)
Functional Sieve
2 2018 1916 1714 1512 1310 118 96 74 53
3 5 7 9 11 13 15 17 19
5 7 11 13 17 19
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
of Eratosthenes
![Page 58: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/58.jpg)
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
Performance differences matter
Haskell:primes = sieve [2..]sieve [] = []sieve (p : xs) = p : sieve [x | x <− xs, x ‘mod‘ p > 0]
O'Neill, Melissa E. The Genuine Sieve of Eratosthenes. Journal of Functional Programming, Vol. 19, No. 1. (2009), pp. 95-106
Functional Sieve Is Not the Real Sieve
![Page 59: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/59.jpg)
Performance differences matter
Haskell:primes = sieve [2..]sieve [] = []sieve (p : xs) = p : sieve [x | x <− xs, x ‘mod‘ p > 0]
Swift:func sieve(numbers: [Int]) -> [Int] { if numbers.isEmpty { return [] } let p = numbers[0] return [p] + sieve(numbers[1..<numbers.count].filter { $0 % p > 0 })}
O'Neill, Melissa E. The Genuine Sieve of Eratosthenes. Journal of Functional Programming, Vol. 19, No. 1. (2009), pp. 95-106
Functional Sieve Is Not the Real Sieve
![Page 60: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/60.jpg)
Immutability in Cocoa[Touch]
Cocoa[Touch] has a number of immutable classes• NSDate, NSURL, UIImage, NSNumber, etc.• Improved safety (no need to use copy)
![Page 61: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/61.jpg)
Immutability in Cocoa[Touch]
Cocoa[Touch] has a number of immutable classes• NSDate, NSURL, UIImage, NSNumber, etc.• Improved safety (no need to use copy)
Downsides to immutabilityNSURL *url = [[NSURL alloc] initWithString: NSHomeDirectory()];NSString *component;while ((component = getNextSubdir()) { url = [url URLByAppendingPathComponent: component];}
![Page 62: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/62.jpg)
Immutability in Cocoa[Touch]
Cocoa[Touch] has a number of immutable classes• NSDate, NSURL, UIImage, NSNumber, etc.• Improved safety (no need to use copy)
Downsides to immutabilityNSArray<NSString *> *array = [NSArray arrayWithObject: NSHomeDirectory()];NSString *component;while ((component = getNextSubdir()) { array = [array arrayByAddingObject: component];}url = [NSURL fileURLWithPathComponents: array];
![Page 63: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/63.jpg)
Thoughtful Mutability in Cocoa[Touch]
Cocoa[Touch] has a number of immutable classes• NSDate, NSURL, UIImage, NSNumber, etc.• Improved safety (no need to use copy)
Thoughtful mutabilityNSMutableArray<NSString *> *array = [NSMutableArray array];[array addObject: NSHomeDirectory()];NSString *component;while ((component = getNextSubdir()) { [array addObject: component];}url = [NSURL fileURLWithPathComponents: array];
You’d miss it if it were gone
![Page 64: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/64.jpg)
Value Semantics
![Page 65: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/65.jpg)
Variables Are Logically Distinct
Mutating one variable of some value type will never affect a different variablevar a: Int = 17 var b = aassert(a == b)
b += 25
print("a = \(a), b = \(b)") // a = 17, b = 42
Integers are value types
![Page 66: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/66.jpg)
Variables Are Logically Distinct
Mutating one variable of some value type will never affect a different variablevar a: CGPoint = CGPoint(x: 3, y: 5)var b = aassert(a == b)
b.x = 17
print("a = \(a), b = \(b)") // a = (x = 3, y = 5), b = (x = 17, y = 5)
CGPoints are value types
![Page 67: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/67.jpg)
Variables Are Logically Distinct
Mutating one variable of some value type will never affect a different variablevar a: String = "Hello"var b = aassert(a == b)
b.extend(" WWDC!")
print("a = \(a), b = \(b)") // a = Hello, b = Hello WWDC!
Strings are value types
![Page 68: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/68.jpg)
Variables Are Logically Distinct
Mutating one variable of some value type will never affect a different variablevar a: [Int] = [1, 2, 3, 4, 5]var b = aassert(a == b)
b[2] = 17
print("a = \(a), b = \(b)") // a = [1, 2, 3, 4, 5], b = [1, 2, 17, 4, 5]
Arrays are value types
![Page 69: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/69.jpg)
Variables Are Logically Distinct
Mutating one variable of some value type will never affect a different variablevar a: [Int : String] = [1 : "uno", 2 : "dos"]var b = aassert(a == b)
b[2] = "due"
print("a = \(a), b = \(b)") // a = [1 : "uno", 2 : "dos"],
// b = [1 : "uno", 2 : "due"]
Dictionaries are value types
![Page 70: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/70.jpg)
Value Types Compose
All of Swift’s “fundamental” types are value types• Int, Double, String, …
![Page 71: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/71.jpg)
Value Types Compose
All of Swift’s “fundamental” types are value types• Int, Double, String, …
All of Swift’s collections are value types• Array, Set, Dictionary, …
![Page 72: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/72.jpg)
Value Types Compose
All of Swift’s “fundamental” types are value types• Int, Double, String, …
All of Swift’s collections are value types• Array, Set, Dictionary, …
Swift tuples, structs, and enums that contain value types are value types
![Page 73: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/73.jpg)
Value Types Are Distinguished by Value
Equality is established by value of a variable• Not its identity• Not how we arrived at the value
var a: Int = 5var b: Int = 2 + 3 assert(a == b)
![Page 74: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/74.jpg)
Value Types Are Distinguished by Value
Equality is established by value of a variable• Not its identity• Not how we arrived at the value
var a: CGPoint = CGPoint(x: 3, y: 5)var b: CGPoint = CGPoint(x: 1, y: 3)b.x += 2 b.y += 2 assert(a == b)
![Page 75: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/75.jpg)
Value Types Are Distinguished by Value
Equality is established by value of a variable• Not its identity• Not how we arrived at the value
var a: String = "Hello WWDC!"var b: String = "Hello" b += " " b += "WWDC!"assert(a == b)
![Page 76: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/76.jpg)
Value Types Are Distinguished by Value
Equality is established by value of a variable• Not its identity• Not how we arrived at the value
var a: [Int] = [1, 2, 3]var b: [Int] = [3, 2, 1].sort(<)assert(a == b)
![Page 77: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/77.jpg)
EquatableValue types should implement Equatable
protocol Equatable { /// Reflexive - `x == x` is `true` /// Symmetric - `x == y` then `y == x` /// Transitive - `x == y` and `y == z` then `x == z` func ==(lhs: Self, rhs: Self) -> Bool }
![Page 78: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/78.jpg)
EquatableValue types should implement Equatable
protocol Equatable { /// Reflexive - `x == x` is `true` /// Symmetric - `x == y` then `y == x` /// Transitive - `x == y` and `y == z` then `x == z` func ==(lhs: Self, rhs: Self) -> Bool }
var a = …var b = aassert(a == b)assert(b == a)var c = bassert(c == a)
![Page 79: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/79.jpg)
Implementing Equatable
protocol Equatable { /// Reflexive - `x == x` is `true` /// Symmetric - `x == y` then `y == x` /// Transitive - `x == y` and `y == z` then `x == z` func ==(lhs: Self, rhs: Self) -> Bool }
extension CGPoint: Equatable { }
func ==(lhs: CGPoint, rhs: CGPoint) -> Bool { return lhs.x == rhs.x && lhs.y == rhs.y }
![Page 80: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/80.jpg)
Value Semantics Temperature
struct Temperature: Equatable { var celsius: Double = 0 var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } }
func ==(lhs: Temperature, rhs: Temperature) -> Bool { return lhs.celsius == rhs.celsius }
![Page 81: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/81.jpg)
Using Value Semantics Temperature
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
![Page 82: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/82.jpg)
Using Value Semantics Temperature
let home = House() let temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
error: cannot assign to property: ‘temp’ is a ‘let’ constant
![Page 83: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/83.jpg)
Using Value Semantics Temperature
let home = House() var temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
![Page 84: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/84.jpg)
home = House() var temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
Using Value Semantics Temperature
House
thermostatoven
let ThermostatTemperaturecelsius: 23.88
OvenTemperaturecelsius: 218.3
![Page 85: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/85.jpg)
Using Value Semantics Temperature
home = House() var temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
Houselet
ThermostatTemperaturecelsius: 23.88
OvenTemperaturecelsius: 218.3
Everywhere
![Page 86: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/86.jpg)
Using Value Semantics Temperature
home = House() var temp = Temperature() temp.fahrenheit = 75 home.thermostat.temperature = temp
temp.fahrenheit = 425 home.oven.temperature = temp home.oven.bake()
House
ThermostatTemperaturecelsius: 23.88
OvenTemperaturecelsius: 218.3
Everywhere
var
![Page 87: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/87.jpg)
Mutation When You Want ItBut not when you don’t
let means “the value will never change”let numbers = [1, 2, 3, 4, 5]
var means you can update the value without affecting any other valuesvar strings = [String]()for x in numbers { strings.append(String(x))}
![Page 88: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/88.jpg)
Freedom from Race Conditions
var numbers = [1, 2, 3, 4, 5]
scheduler.processNumbersAsynchronously(numbers)
for i in 0..<numbers.count { numbers[i] = numbers[i] * i }
scheduler.processNumbersAsynchronously(numbers)
![Page 89: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/89.jpg)
Performance
var numbers = [1, 2, 3, 4, 5]
scheduler.processNumbersAsynchronously(numbers)
for i in 0..<numbers.count { numbers[i] = numbers[i] * i }
scheduler.processNumbersAsynchronously(numbers)
What about all those copies?
![Page 90: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/90.jpg)
Copies Are Cheap
![Page 91: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/91.jpg)
Copies Are CheapConstant time
![Page 92: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/92.jpg)
Copies Are Cheap
Copying a low-level, fundamental type is constant time• Int, Double, etc.
Constant time
![Page 93: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/93.jpg)
Copies Are Cheap
Copying a low-level, fundamental type is constant time• Int, Double, etc.
Copying a struct, enum, or tuple of value types is constant time• CGPoint, etc.
Constant time
![Page 94: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/94.jpg)
Copies Are Cheap
Copying a low-level, fundamental type is constant time• Int, Double, etc.
Copying a struct, enum, or tuple of value types is constant time• CGPoint, etc.
Extensible data structures use copy-on-write• Copying involves a fixed number of reference-counting operations• String, Array, Set, Dictionary, etc.
Constant time
![Page 95: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/95.jpg)
Value Semantics Are Simple and Efficient
Different variables are logically distinctMutability when you want itCopies are cheap
![Page 96: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/96.jpg)
Value Types in Practice
Conceptualize an Example
![Page 97: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/97.jpg)
A Diagram Made of Value Types
Circle
Polygon
Diagram
![Page 98: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/98.jpg)
Circle
struct Circle: Equatable { var center: CGPoint var radius: Double init(center: CGPoint, radius: Double) { self.center = center self.radius = radius } }
func ==(lhs: Circle, rhs: Circle) { return lhs.center == rhs.center && lhs.radius == rhs.radius }
Circle
CGFloatPoint
CGFloat CGFloat
![Page 99: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/99.jpg)
struct Polygon: Equatable { var corners: [CGPoint] = [] }
func ==(lhs: Polygon, rhs: Polygon) { return lhs.corners == rhs.corners }
Polygon
Polygon
PointCGFloat
PointCGFloat
PointCGFloat
CGFloat
CGFloat
CGFloat
![Page 100: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/100.jpg)
PolygonCircle
Diagram Contains Circles
0..n Diagram
![Page 101: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/101.jpg)
Diagram Contains Polygons
Polygon Diagram0..n
Circle
![Page 102: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/102.jpg)
Diagram Contains Polygons
?
Polygon
Diagram0..n
Circle
![Page 103: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/103.jpg)
Drawable
Diagram Contains Polygons
Diagram
Protocols can abstract over value types
Circle Polygon
0..n
![Page 104: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/104.jpg)
Drawable
Diagram Contains Polygons
Diagram
Protocols can abstract over value types
Circle Polygon
Protocol-Oriented Programming in Swift (Repeat) Pacific Heights Friday 3:30PM
0..n
![Page 105: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/105.jpg)
The Drawable Protocol
protocol Drawable { func draw() }
![Page 106: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/106.jpg)
The Drawable Protocol
protocol Drawable { func draw() }
extension Polygon: Drawable { func draw() {
let ctx = UIGraphicsGetCurrentContext() CGContextMoveToPoint(ctx, corners.last!.x corners.last!.y) for point in corners { CGContextAddLineToPoint(ctx, point.x, point.y) } CGContextClosePath(ctx) CGContextStrokePath(ctx)
} }
![Page 107: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/107.jpg)
extension Circle: Drawable { func draw() { let arc = CGPathCreateMutable() CGPathAddArc(arc, nil, center.x, center.y, radius, 0, 2 * π, true) CGContextAddPath(ctx, arc) CGContextStrokePath(ctx) } }
protocol Drawable { func draw() }
The Drawable Protocol
![Page 108: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/108.jpg)
Creating the Diagram
struct Diagram { var items: [Drawable] = []
}
![Page 109: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/109.jpg)
Creating the Diagram
struct Diagram { var items: [Drawable] = []
mutating func addItem(item: Drawable) { items.append(item) }
}
![Page 110: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/110.jpg)
Creating the Diagram
struct Diagram { var items: [Drawable] = []
mutating func addItem(item: Drawable) { items.append(item) }
func draw() { for item in items { item.draw() } } }
![Page 111: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/111.jpg)
Adding Items
Diagramdoc
var doc = Diagram()
![Page 112: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/112.jpg)
Adding Items
Diagramdoc
Polygondoc.addItem(Polygon())var doc = Diagram()
![Page 113: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/113.jpg)
Adding Items
Diagramdoc
Polygon
Circle
doc.addItem(Circle())doc.addItem(Polygon())var doc = Diagram()
![Page 114: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/114.jpg)
Copied on Assignment
Diagramdoc
Polygon
Circle
Diagramdoc2
Polygon
Circle
doc.addItem(Circle())doc.addItem(Polygon())var doc = Diagram()
var doc2 = doc
![Page 115: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/115.jpg)
Diagram
Copied on Assignment
doc
Polygon
Circle
Diagramdoc2
Polygon
Polygon
Heterogeneous arrays have value semantics, too!
doc.addItem(Circle())doc.addItem(Polygon())var doc = Diagram()
var doc2 = docdoc2.items[1] = Polygon(corners: points)
![Page 116: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/116.jpg)
Making Diagram Equatable
extension Diagram: Equatable { }
func ==(lhs: Diagram, rhs: Diagram) { return lhs.items == rhs.items}
![Page 117: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/117.jpg)
Making Diagram Equatable
extension Diagram: Equatable { }
func ==(lhs: Diagram, rhs: Diagram) { return lhs.items == rhs.items}
error: binary operator ‘==‘ cannot be applied to two [Drawable] operands
![Page 118: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/118.jpg)
Making Diagram Equatable
extension Diagram: Equatable { }
func ==(lhs: Diagram, rhs: Diagram) { return lhs.items == rhs.items}
Protocol-Oriented Programming in Swift (Repeat) Pacific Heights Friday 3:30PM
error: binary operator ‘==‘ cannot be applied to two [Drawable] operands
![Page 119: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/119.jpg)
If It Quacks Like a Duck…
protocol Drawable { func draw() }
struct Diagram { var items: [Drawable] = []
func draw() { … } }
![Page 120: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/120.jpg)
If It Quacks Like a Duck…
protocol Drawable { func draw() }
struct Diagram: Drawable { var items: [Drawable] = []
func draw() { … } }
![Page 121: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/121.jpg)
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle())
Diagram as a Drawable
Diagramdoc
Polygon
Circle
![Page 122: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/122.jpg)
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle()) doc.addItem(Diagram())
Diagramdoc
Polygon
Circle
Diagram
Diagram as a Drawable
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle())
![Page 123: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/123.jpg)
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle())
Diagram as a Drawable
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle()) doc.addItem(doc)
Diagram
Polygon
Circle
doc
![Page 124: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/124.jpg)
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle())
Diagram as a Drawable
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle()) doc.addItem(doc)
func draw() { for item in items { item.draw() } magicmove } magicmove
Diagram
Polygon
Circle
doc
![Page 125: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/125.jpg)
Diagram as a Drawable
Diagramdoc
Polygon
Circle
Diagram
Polygon
Circle
func draw() { for item in items { item.draw() } magicmove } magicmove
var doc = Diagram() doc.addItem(Polygon()) doc.addItem(Circle()) doc.addItem(doc)
![Page 126: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/126.jpg)
Mixing Value Types and Reference Types
![Page 127: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/127.jpg)
Reference Types Often Contain Value Types
Value types generally used for “primitive” data of objectsclass Button : Control { var label: String var enabled: Bool // … }
![Page 128: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/128.jpg)
A Value Type Can Contain a Reference
Copies of the value type will share the referencestruct ButtonWrapper { var button: Button}
![Page 129: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/129.jpg)
A Value Type Can Contain a Reference
Copies of the value type will share the referencestruct ButtonWrapper { var button: Button}
Maintaining value semantics requires special considerations• How do we cope with mutation of the referenced object?• How does the reference identity affect equality?
![Page 130: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/130.jpg)
struct Image : Drawable { var topLeft: CGPoint var image: UIImage }
Immutable References
ImageCGPoint
CGFloat CGFloat UIImage
![Page 131: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/131.jpg)
struct Image : Drawable { var topLeft: CGPoint var image: UIImage }
var image = Image(topLeft: CGPoint(x: 0, y: 0), image: UIImage(imageNamed:"San Francisco")!)
Immutable References
ImageCGPoint
CGFloat CGFloat
image
UIImage
![Page 132: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/132.jpg)
Mutation of the referenced object does not occur
var image2 = image
struct Image : Drawable { var topLeft: CGPoint var image: UIImage }
var image = Image(topLeft: CGPoint(x: 0, y: 0), image: UIImage(imageNamed:"San Francisco”)!)
Immutable References Are Okay!
ImageCGPoint
CGFloat CGFloat
image
UIImageImage
CGPointCGFloat CGFloat
image2
![Page 133: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/133.jpg)
struct Image : Drawable { var topLeft: CGPoint var image: UIImage }
extension Image : Equatable { } func ==(lhs: Image, rhs: Image) -> Bool { return lhs.topLeft == rhs.topLeft && lhs.image === rhs.image }
Immutable References and Equatable
ImageCGPoint
CGFloat CGFloat
image
UIImageImage
CGPointCGFloat CGFloat
image2
struct Image : Drawable { var topLeft: CGPoint var image: UIImage }
![Page 134: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/134.jpg)
struct Image : Drawable { var topLeft: CGPoint var image: UIImage }
extension Image : Equatable { } func ==(lhs: Image, rhs: Image) -> Bool { return lhs.topLeft == rhs.topLeft && lhs.image === rhs.image }
Immutable References and EquatableReference identity is not enough
ImageCGPoint
CGFloat CGFloat
image
UIImage
ImageCGPoint
CGFloat CGFloat
image2
UIImage
![Page 135: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/135.jpg)
struct Image : Drawable { var topLeft: CGPoint var image: UIImage }
extension Image : Equatable { } func ==(lhs: Image, rhs: Image) -> Bool { return lhs.topLeft == rhs.topLeft && lhs.image.isEqual(rhs.image) }
Immutable References and EquatableUse deep equality comparisons
ImageCGPoint
CGFloat CGFloat
image
UIImage
ImageCGPoint
CGFloat CGFloat
image2
UIImage
![Page 136: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/136.jpg)
struct BezierPath: Drawable { var path = UIBezierPath()
var isEmpty: Bool { return path.empty }
path.addLineToPoint(point) } }
func addLineToPoint(point: CGPoint) {
References to Mutable Objects
UIBezierPathBezierPath
![Page 137: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/137.jpg)
struct BezierPath: Drawable { var path = UIBezierPath()
var isEmpty: Bool { return path.empty }
path.addLineToPoint(point) } }
func addLineToPoint(point: CGPoint) {
References to Mutable Objects
UIBezierPathBezierPath
![Page 138: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/138.jpg)
struct BezierPath: Drawable { var path = UIBezierPath()
var isEmpty: Bool { return path.empty }
path.addLineToPoint(point) } }
func addLineToPoint(point: CGPoint) {
References to Mutable Objects
UIBezierPath
BezierPath
BezierPath
Unexpected mutation
![Page 139: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/139.jpg)
Copy-on-Write
Unrestricted mutation of referenced objects breaks value semanticsSeparate non-mutating operations from mutating ones• Non-mutating operations are always safe• Mutating operations must first copy
![Page 140: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/140.jpg)
struct BezierPath: Drawable { private var _path = UIBezierPath() var pathForReading: UIBezierPath { return _path } }
Copy-on-Write in Action
UIBezierPathBezierPath
![Page 141: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/141.jpg)
struct BezierPath: Drawable { private var _path = UIBezierPath() var pathForReading: UIBezierPath { return _path } var pathForWriting: UIBezierPath { mutating get { _path = _path.copy() as! UIBezierPath return _path } } }
Copy-on-Write in Action
UIBezierPathBezierPath
![Page 142: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/142.jpg)
extension BezierPath { var isEmpty: Bool { return pathForReading.empty }
pathForWriting.addLineToPoint(point) } }
Copy-on-Write in Action
UIBezierPathBezierPath
func addLineToPoint(point: CGPoint) {
![Page 143: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/143.jpg)
extension BezierPath { var isEmpty: Bool { return pathForReading.empty }
pathForWriting.addLineToPoint(point) } }
Copy-on-Write in Action
UIBezierPathBezierPath
func addLineToPoint(point: CGPoint) {
error: cannot read ‘pathForWriting’ because ‘self’ is not mutable
![Page 144: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/144.jpg)
extension BezierPath { var isEmpty: Bool { return pathForReading.empty }
pathForWriting.addLineToPoint(point) } }
Copy-on-Write in Action
UIBezierPathBezierPath
func addLineToPoint(point: CGPoint) {mutating
![Page 145: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/145.jpg)
Bezier PathCopy-on-write
path
var path = BezierPath()
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPathBezierPath
![Page 146: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/146.jpg)
Bezier PathCopy-on-write
path
var path = BezierPath()
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPath
BezierPath
var path2 = path
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
path2 BezierPath
![Page 147: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/147.jpg)
Bezier PathCopy-on-write
path
var path = BezierPath()
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPath
BezierPath
var path2 = path
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
path2 BezierPath
if path.empty { print("Path is empty") }
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPath
![Page 148: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/148.jpg)
Bezier PathCopy-on-write
path
var path = BezierPath()
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPath
BezierPath
var path2 = path
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
path2 BezierPath
if path.empty { print("Path is empty") }
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPath
path.addLineToPoint(CGPoint(x: 10, y: 20))
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
![Page 149: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/149.jpg)
Bezier PathCopy-on-write
path
var path = BezierPath()
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPath
BezierPath
var path2 = path
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
path2 BezierPath
if path.empty { print("Path is empty") }
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
UIBezierPath
path.addLineToPoint(CGPoint(x: 10, y: 20))
var path2 = path path.addLineToPoint(CGPoint(x: 100, y: 125))
![Page 150: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/150.jpg)
Forming a Path from a Polygon
extension Polygon { var path: BezierPath { var result = BezierPath() result.moveToPoint(corners.last!) for point in corners { result.addLineToPoint(point) } return result } }
![Page 151: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/151.jpg)
Forming a Path from a PolygonCopies every time through the loop!
extension Polygon { var path: BezierPath { var result = BezierPath() result.moveToPoint(corners.last!) for point in corners { result.addLineToPoint(point) } return result } }
![Page 152: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/152.jpg)
Forming a Path from a PolygonUse the mutable reference type (carefully)
extension Polygon { var path: BezierPath { var result = UIBezierPath() result.moveToPoint(corners.last!) for point in corners { result.addLineToPoint(point) } return BezierPath(path: result) } }
![Page 153: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/153.jpg)
Uniquely Referenced Swift Objects
struct MyWrapper { var _object: SomeSwiftObject var objectForWriting: SomeSwiftObject { mutating get {
return _object } } }
_object = _object.copy()
![Page 154: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/154.jpg)
Uniquely Referenced Swift Objects
struct MyWrapper { var _object: SomeSwiftObject var objectForWriting: SomeSwiftObject { mutating get {
return _object } } }
if !isUniquelyReferencedNonObjC(&_object)) {
} _object = _object.copy()
![Page 155: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/155.jpg)
Uniquely Referenced Swift Objects
struct MyWrapper { var _object: SomeSwiftObject var objectForWriting: SomeSwiftObject { mutating get {
return _object } } }
if !isUniquelyReferencedNonObjC(&_object)) {
} _object = _object.copy()
The standard library value types uses this throughout
![Page 156: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/156.jpg)
Mixing Value Types and Reference Types
Maintaining value semantics requires special considerationsCopy-on-write enables efficient value semantics when wrapping Swift reference types
![Page 157: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/157.jpg)
Implementing Undo with Value Types
![Page 158: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/158.jpg)
Undo
var doc = Diagram() var undoStack: [Diagram] = []
Diagram
undoStack
doc
![Page 159: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/159.jpg)
var doc = Diagram() var undoStack: [Diagram] = []undoStack.append(doc)
Undo
Diagram
DiagramundoStack
doc
![Page 160: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/160.jpg)
Undo
var doc = Diagram() var undoStack: [Diagram] = [] Diagram
DiagramundoStack
Polygon
doc
undoStack.append(doc)doc.addItem(Polygon())
![Page 161: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/161.jpg)
Undo
var doc = Diagram() var undoStack: [Diagram] = []
Diagram
DiagramundoStack
Polygon
Diagram
Polygon
doc
undoStack.append(doc) doc.addItem(Polygon())undoStack.append(doc)
Diagram
Circle
Polygon
![Page 162: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/162.jpg)
Undo
var doc = Diagram() var undoStack: [Diagram] = []
Diagramdoc
DiagramundoStack
Polygon
Circle
Diagram
PolygonundoStack.append(doc)doc.addItem(Circle())undoStack.append(doc) doc.addItem(Polygon())undoStack.append(doc)
Diagram
Circle
Polygon
![Page 163: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/163.jpg)
History
![Page 164: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/164.jpg)
History
![Page 165: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/165.jpg)
Value SemanticsPhotoshop uses value semantics
Every action results in a doc instanceEfficient because of copy-on-write
Parent, Sean. Value Semantics and Concept-based Polymorphism. C++ Now!, 2012
![Page 166: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/166.jpg)
Value SemanticsPhotoshop uses value semantics
Every action results in a doc instanceEfficient because of copy-on-write
Parent, Sean. Value Semantics and Concept-based Polymorphism. C++ Now!, 2012
![Page 167: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/167.jpg)
Value SemanticsPhotoshop uses value semantics
Every action results in a doc instanceEfficient because of copy-on-write
Parent, Sean. Value Semantics and Concept-based Polymorphism. C++ Now!, 2012
![Page 168: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/168.jpg)
Value SemanticsPhotoshop uses value semantics
Every action results in a doc instanceEfficient because of copy-on-write
Parent, Sean. Value Semantics and Concept-based Polymorphism. C++ Now!, 2012
![Page 169: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/169.jpg)
Summary
Reference semantics and unexpected mutationValue semantics solve these problemsExpressiveness of mutability, safety of immutability
![Page 170: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/170.jpg)
Related Sessions
Protocol-Oriented Programming in Swift Mission Wednesday 2:30PM
Optimizing Swift Performance Presidio Thursday 9:00AM
Protocol-Oriented Programming in Swift (Repeat) Pacific Heights Friday 3:30PM
![Page 171: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/171.jpg)
More Information
Swift Language Documentationhttp://developer.apple.com/swift
Apple Developer Forumshttp://developer.apple.com/forums
Stefan LesserSwift [email protected]
![Page 172: Building Better Apps with Value Types in Swift€¦ · Value Types in Swift Doug Gregor Language Lawyer Bill Dudney Arranger of Bits Developer Tools #WWDC15 Session 414. Roadmap Reference](https://reader034.vdocuments.net/reader034/viewer/2022050611/5fb24405dae999206b2ecf68/html5/thumbnails/172.jpg)