Slide 1

Slide 1 text

۩ମྫͱΫΠζͰֶͿɺ Swi$ͷ4छྨͷΤϥʔͷ࢖͍෼͚ Yuta Koshizawa @koher

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Q. Swi' ͰΤϥʔΛදͦ͏ͱࢥͬͨ ͱ͖ʹԿΛ࢖͍·͔͢ʁ

Slide 8

Slide 8 text

throw FooError()

Slide 9

Slide 9 text

let number: Int? = Int(string)

Slide 10

Slide 10 text

let foo: Foo? = try? foo()

Slide 11

Slide 11 text

fatalError()

Slide 12

Slide 12 text

assert(...)

Slide 13

Slide 13 text

precondition(...)

Slide 14

Slide 14 text

assertionFailure()

Slide 15

Slide 15 text

preconditionFailure()

Slide 16

Slide 16 text

fooOrNil!

Slide 17

Slide 17 text

array[-1]

Slide 18

Slide 18 text

Int.max + 1

Slide 19

Slide 19 text

[Int](repeating: 42, count: Int.max)

Slide 20

Slide 20 text

func foo() { foo() }

Slide 21

Slide 21 text

throw FooError() let number: Int? = Int(String) let foo: Foo? = try? foo() fatalError() assert(...) assertionFailure() precondition(...) preconditionFailure() fooOrNil! array[-1] Int.max + 1 [Int](repeating: 42, count: Int.max) func foo() { foo() }

Slide 22

Slide 22 text

Javaͷ৔߹ Throwable -+- Exception -+- RuntimeException -+- NullPointerException | | +- IndexOutOfBoundsException | | ... | | | +- IOException | +- SQLException | ... | +- Error - ... -+- OutOfMemoryError +- StackOverflowError ...

Slide 23

Slide 23 text

throw FooError() let number: Int? = Int(string) let foo: Foo? = try? foo() fatalError() assert(...) assertionFailure() precondition(...) preconditionFailure() fooOrNil! array[-1] Int.max + 1 [Int](repeating: 42, count: Int.max) func foo() { foo() }

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Simple domain errors Recoverable errors Universal errors Logic failures

Slide 30

Slide 30 text

Simple domain errors

Slide 31

Slide 31 text

let number: Int? = Int(string)

Slide 32

Slide 32 text

Simple domain errors • ΦϖϨʔγϣϯ͕ࣦഊ͢Δલఏ৚͕݅໌֬ • ੒ޭ͔ࣦͨ͠ഊ͔͕ͨ͠෼͔Ε͹े෼ • Swi% Ͱ͸ Optional Λฦ͢͜ͱͰද͢

Slide 33

Slide 33 text

let element: Int? = array.first

Slide 34

Slide 34 text

let value = dictionary["unknownKey"]

Slide 35

Slide 35 text

Recoverable errors

Slide 36

Slide 36 text

try fileManager .removeItem(at: url)

Slide 37

Slide 37 text

Recoverable errors • ༷ʑͳݪҼͰΦϖϨʔγϣϯ͕ࣦഊ͢Δ • ࣦഊͷݪҼΛ஌Γ͍ͨ • ݪҼผʹϋϯυϦϯά͍ͨ͠ • Swi% Ͱ͸ Error Λ throw ͢Δ͜ͱͰද͢

Slide 38

Slide 38 text

ճ෮ՄೳͳΤϥʔ Simple domain errors Recoverable errors

Slide 39

Slide 39 text

ճ෮ෆೳͳΤϥʔ Universal errors Logic failures

Slide 40

Slide 40 text

Universal errors

Slide 41

Slide 41 text

[Int](repeating: 42, count: Int.max)

Slide 42

Slide 42 text

Universal error ͕ϋϯυϦϯάՄೳͩͬͨͱ͢Δͱ // ϝϞϦෆ଍ͷϋϯυϦϯάͷྫ let foo: Foo? = allocate(size(of: Foo)) guard let foo = foo else { // Τϥʔϝοηʔδͷੜ੒Ͱ͞ΒʹϝϞϦෆ଍͕ى͜Δ͔΋ showAlert("ϝϞϦ͕଍Γ·ͤΜɻ") }

Slide 43

Slide 43 text

func foo() { foo() }

Slide 44

Slide 44 text

Ctrl + C

Slide 45

Slide 45 text

thread.cancel()

Slide 46

Slide 46 text

Universal errors • ίʔυதͷࢸΔͱ͜ΖͰൃੜ͢Δ΋ͷ͕ଟ͍ • ϋϯυϦϯά͠Α͏͕ͳ͍ • ίʔυ͔Βൃੜͤ͞Δʹ͸ NSException ʢ·ͨ͸ fatalError ʣ

Slide 47

Slide 47 text

Logic failures

Slide 48

Slide 48 text

fooOrNil!

Slide 49

Slide 49 text

Int.max + 1

Slide 50

Slide 50 text

array[-1]

Slide 51

Slide 51 text

Array ͷ subscript ͷࣦഊ͕ Logic failure ͳཧ༝ // [Int] Λιʔτ͢Δؔ਺ func sort(_ ns: inout [Int]) { for i in 0 ..< ns.count { for j in i + 1 ..< ns.count { if ns[j] < ns[i] { let t = ns[j] ns[j] = ns[i] ns[i] = t } } } }

Slide 52

Slide 52 text

Array ͷ subscript ͷࣦഊ͕ Logic failure ͳཧ༝ // [Int] Λιʔτ͢Δؔ਺ func sort(_ ns: inout [Int]) { for i in 0 ..< ns.count { for j in i + 1 ..< ns.count { if ns[j] < ns[i] { let t = ns[j] ns[j] = ns[i] ns[i] = t } } } }

Slide 53

Slide 53 text

Array ͷ subscript ͷࣦഊ͕ Logic failure ͳཧ༝ // ..< Λ ... ʹͯ͠͠·͏ͱΠϯσοΫε͕͸Έग़ͯΤϥʔ func sort(_ ns: inout [Int]) { for i in 0 ... ns.count { for j in i + 1 ... ns.count { if ns[j] < ns[i] { let t = ns[j] ns[j] = ns[i] ns[i] = t } } } }

Slide 54

Slide 54 text

Array ͷ subscript ͷࣦഊ͕ Logic failure ͳཧ༝ // Ͱ΋ɺ͜͜Ͱ nil ͕ฦ͖ͬͯͯ΋ϋϯυϦϯά͠Α͏͕ͳ͍ func sort(_ ns: inout [Int]) { for i in 0 ... ns.count { for j in i + 1 ... ns.count { if ns[j] < ns[i] { let t = ns[j] ns[j] = ns[i] ns[i] = t } } } }

Slide 55

Slide 55 text

Array ͷ subscript ͷࣦഊ͕ Logic failure ͳཧ༝ // Ͳ͏ͤ ! ͢Δ͜ͱʹͳͬͯແବ func sort(_ ns: inout [Int]) { for i in 0 ... ns.count { for j in i + 1 ... ns.count { if ns[j]! < ns[i]! { let t = ns[j]! ns[j] = ns[i]! ns[i] = t } } } }

Slide 56

Slide 56 text

Logic failures • ίʔυͷϛεʹΑͬͯൃੜ • ࣮ߦ࣌Ͱ͸ͳ͘ίʔυͷमਖ਼ʹΑͬͯରԠ • precondition, assert ͳͲͰൃੜͤ͞Δ

Slide 57

Slide 57 text

ؔ਺ -Onone -O -Ounchecked fatalError ○ ○ ○ precondi/on ○ ○ assert ○ ※ ○͕෇͍͍ͯͳ͍૊Έ߹Θͤ͸νΣοΫ͕লུ͞ΕΔ

Slide 58

Slide 58 text

precondition ͩͱ͏Ε͍͠ཧ༝ // [Int] Λιʔτ͢Δؔ਺ func sort(_ ns: inout [Int]) { for i in 0 ..< ns.count { for j in i + 1 ..< ns.count { if ns[j] < ns[i] { let t = ns[j] ns[j] = ns[i] ns[i] = t } } } }

Slide 59

Slide 59 text

precondition ͩͱ͏Ε͍͠ཧ༝ // [Int] Λιʔτ͢Δؔ਺ func sort(_ ns: inout [Int]) { for i in 0 ..< ns.count { for j in i + 1 ..< ns.count { if ns[j] < ns[i] { let t = ns[j] ns[j] = ns[i] ns[i] = t } } } }

Slide 60

Slide 60 text

precondition ͩͱ͏Ε͍͠ཧ༝ // guard ͱ fatalError ͰΠϯσοΫεͷνΣοΫ struct Array { subscript(index: Int): Element { get { guard 0 <= index && index < count else { fatalError() } ... } set { ... } } }

Slide 61

Slide 61 text

precondition ͩͱ͏Ε͍͠ཧ༝ // guard ͱ fatalError ͰΠϯσοΫεͷνΣοΫ struct Array { subscript(index: Int): Element { get { guard 0 <= index && index < count else { fatalError() } ... } set { ... } } }

Slide 62

Slide 62 text

precondition ͩͱ͏Ε͍͠ཧ༝ // precondition ͸ -Ounchecked ͰऔΓআ͔ΕΔ struct Array { subscript(index: Int): Element { get { precondition(0 <= index && index < count) ... } set { ... } } }

Slide 63

Slide 63 text

-Onone ͷͱ͖ͷڍಈ // arrayindex.swift let a = [2, 3, 5] print(a[3]) $ swift arrayindex.swift fatal error: Index out of range

Slide 64

Slide 64 text

-Ounchecked ͷͱ͖ͷڍಈ // arrayindex.swift let a = [2, 3, 5] print(a[3]) $ swift -Ounchecked arrayindex.swift 2392130230941712

Slide 65

Slide 65 text

-Ounchecked ͷͱ͖ͷڍಈ let a: Int? = nil print(a!) // ࣮ߦ࣌ΤϥʔʹͳΒͳ͍ let b: UInt8 = 255 print(b + 1) // ࣮ߦ࣌ΤϥʔʹͳΒͳ͍

Slide 66

Slide 66 text

Logic failure ͸ίʔυͷϛεͷΑͬ ͯҾ͖ى͜͞ΕΔ ↕ ίʔυͷϛε͕ͳ͚Ε͹ Logic failure ͸ى͜Βͳ͍

Slide 67

Slide 67 text

assert ͷ࢖༻ྫ assert ͸಺෦తͳ੔߹ੑͷνΣοΫʹ࢖͏ import TensorSwift // 10000 ਓ෼ͷ྆खͷࢦͷ௕͞ let tensor = Tensor(shape: [10000, 2, 5], elements: ...) // [ਓ, ࠨӈ, ࢦ] ͔Β [ࢦ, ਓ, ࠨӈ] ʹม׵ let transposed = tensor.transposed([2, 0, 1]) // ࠨखͷσʔλ͚ͩΛऔΓग़͢ let lefthand = tensor[..., ..., 1] // [ࢦ, ਓ] ͷॱʹͳ͍ͬͯͯ΄͍͚͠Ͳॲཧ͕ෳࡶͰࣗ৴͕࣋ͯͳ͍ͷͰνΣοΫ assert(lefthand.shape == [5, 10000])

Slide 68

Slide 68 text

Simple domain errors Recoverable errors Universal errors Logic failures

Slide 69

Slide 69 text

ΤϥʔͷछྨͷܾΊํ ͲΜͳ಺༰ͷΤϥʔ͔ ͲͷΑ͏ʹΤϥʔϋϯυϦϯά͍͔ͤͨ͞

Slide 70

Slide 70 text

// Simple domain error Ͱͳ͘ Recoverable Ͱ͋ͬͯ΄͍͠ let string = "99999999999999999999999999" ... do { let number: Int = try Int(string) } catch is OverflowError { showAlert("\(Int.min) - \(Int.max) ͷ஋Λೖྗͯ͠Լ͍͞ɻ") } catch { showAlert("੔਺Λೖྗͯ͠Լ͍͞ɻ") }

Slide 71

Slide 71 text

// Universal error Ͱ͸ͳ͘ Simple domain error Ͱ͋ͬͯ΄͍͠ guard let a = malloc(MemoryLayout.size * 100_000_000) else { showAlert("ϝϞϦ͕଍Γ·ͤΜɻ") return } // a Λ࢖͏ॲཧ

Slide 72

Slide 72 text

// Simple domain error → Logic failure let number: Int = Int(string)!

Slide 73

Slide 73 text

from \ to SDE RE UE LF SDE - guard let throw guard let fatalError ! RE try? - do-catch fatalError try! UE - LF if/guard return nil if/guard throw if/guard fatalError - SDE: Simple domain error, RE: Recoverable error, UE: Universal error, LF: Logic failure

Slide 74

Slide 74 text

// Ͳ͏ͤ ! ͢Δ͜ͱʹͳͬͯແବ func sort(_ ns: inout [Int]) { for i in 0 ... ns.count { for j in i + 1 ... ns.count { if ns[j]! < ns[i]! { let t = ns[j]! ns[j] = ns[i]! ns[i] = t } } } }

Slide 75

Slide 75 text

ΫΠζλΠϜ

Slide 76

Slide 76 text

ΫΠζͷϧʔϧ - ࡾ୒ܗࣜ - ੍ݶ࣌ؒ͸ 30 ඵ - શ෦Ͱ 5 ໰ - ඞͣࡾ୒ͷͲΕ͔ʹखΛڍ͛Δ

Slide 77

Slide 77 text

ΫΠζͷϧʔϧ - ࡾ୒ܗࣜ - ੍ݶ࣌ؒ͸ 30 ඵ - શ෦Ͱ 5 ໰ - ඞͣࡾ୒ͷͲΕ͔ʹखΛڍ͛Δ

Slide 78

Slide 78 text

ΫΠζͷϧʔϧ - ࡾ୒ܗࣜ - ੍ݶ࣌ؒ͸ 30 ඵ - શ෦Ͱ 5 ໰ - ඞͣࡾ୒ͷͲΕ͔ʹखΛڍ͛Δ

Slide 79

Slide 79 text

ΫΠζͷϧʔϧ - ࡾ୒ܗࣜ - ੍ݶ࣌ؒ͸ 30 ඵ - શ෦Ͱ 5 ໰ - ඞͣࡾ୒ͷͲΕ͔ʹखΛڍ͛Δ

Slide 80

Slide 80 text

@koher h"ps:/ /twi"er.com/koher

Slide 81

Slide 81 text

Q1. ϦόʔγͷϘʔυ

Slide 82

Slide 82 text

let disk: Disk? = board[x, y]

Slide 83

Slide 83 text

1. Simple domain error struct Board { subscript(x: Int, y: Int) -> Disk? { guard (0..<8).contains(x) else { return nil } guard (0..<8).contains(y) else { return nil } ... } ... }

Slide 84

Slide 84 text

2. Universal error struct Board { subscript(x: Int, y: Int) -> Disk? { guard (0..<8).contains(x) else { fatalError() } guard (0..<8).contains(y) else { fatalError() } ... } ... }

Slide 85

Slide 85 text

3. Logic failure struct Board { subscript(x: Int, y: Int) -> Disk? { precondition((0..<8).contains(x)) precondition((0..<8).contains(y)) ... } ... }

Slide 86

Slide 86 text

γϯΩϯάλΠϜ 1. Simple domain error 2. Universal error 3. Logic failure

Slide 87

Slide 87 text

౴͑ 1. Simple domain error 2. Universal error 3. Logic failure

Slide 88

Slide 88 text

| ○○○●|

Slide 89

Slide 89 text

| ●○○○●|

Slide 90

Slide 90 text

| ○○○ |

Slide 91

Slide 91 text

| ●○○○ |

Slide 92

Slide 92 text

| ○○○|

Slide 93

Slide 93 text

| ●○○○|

Slide 94

Slide 94 text

struct Board { subscript(x: Int, y: Int) -> Disk? { guard (0..<8).contains(x) else { return nil } guard (0..<8).contains(y) else { return nil } ... } ... }

Slide 95

Slide 95 text

struct Board { subscript(x: Int, y: Int) -> Disk? { precondition((0..<8).contains(x)) precondition((0..<8).contains(y)) ... } ... }

Slide 96

Slide 96 text

Q2. ΞϓϦʹόϯυϧ͞Εͨσʔλ

Slide 97

Slide 97 text

let foo: Foo = loadFoo()

Slide 98

Slide 98 text

1. Recoverable error func loadFoo() throws -> Foo { guard let path = Bundle.main.path(forResource: "Foo", ofType: "json") else { throw ... } let url = URL(fileURLWithPath: path) let data = try Data(contentsOf: url) return try JSONDecoder().decode(Foo.self, from: data) }

Slide 99

Slide 99 text

2. Universal error func loadFoo() -> Foo { guard let path = Bundle.main.path(forResource: "Foo", ofType: "json") else { fatalError() } let url = URL(fileURLWithPath: path) do { let data = try Data(contentsOf: url) return try JSONDecoder().decode(Foo.self, from: data) } catch { fatalError() } }

Slide 100

Slide 100 text

3. Logic failure func loadFoo() -> Foo { let path = Bundle.main.path(forResource: "Foo", ofType: "json")! let url = URL(fileURLWithPath: path) let data = try! Data(contentsOf: url) return try! JSONDecoder().decode(Foo.self, from: data) }

Slide 101

Slide 101 text

γϯΩϯάλΠϜ 1. Recoverable error 2. Universal error 3. Logic failure

Slide 102

Slide 102 text

౴͑ 1. Recoverable error 2. Universal error 3. Logic failure

Slide 103

Slide 103 text

guard let image = UIImage(named: "Foo") else { // ΤϥʔϋϯυϦϯά ... } // image Λ࢖͏ॲཧ

Slide 104

Slide 104 text

let image = UIImage(named: "Foo")! // image Λ࢖͏ॲཧ

Slide 105

Slide 105 text

Q3. ηϯαʔ͔Βͷσʔλऔಘ

Slide 106

Slide 106 text

let foo: Foo = readFoo()

Slide 107

Slide 107 text

1. Recoverable error func readFoo() throws -> Foo { guard isFooSensorAvailable() else { throw ... } ... }

Slide 108

Slide 108 text

2. Universal error func readFoo() -> Foo { guard isFooSensorAvailable() else { fatalError() } ... }

Slide 109

Slide 109 text

3. Logic failure func readFoo() -> Foo { precondition(isFooSensorAvailable()) ... }

Slide 110

Slide 110 text

γϯΩϯάλΠϜ 1. Recoverable error 2. Universal error 3. Logic failure

Slide 111

Slide 111 text

౴͑ 1. Recoverable error 2. Universal error 3. Logic failure

Slide 112

Slide 112 text

Q4. ήʔϜͷηʔϒσʔλ

Slide 113

Slide 113 text

let game: Game? = loadGame()

Slide 114

Slide 114 text

1. Recoverable error func loadGame() throws -> Game? { ... guard hash == savedHash else { throw ... } ... }

Slide 115

Slide 115 text

2. Universal error func loadGame() -> Game? { ... guard hash == savedHash else { fatalError() } ... }

Slide 116

Slide 116 text

3. Logic failure func loadGame() -> Game? { ... precondition(hash == savedHash) ... }

Slide 117

Slide 117 text

γϯΩϯάλΠϜ 1. Recoverable error 2. Universal error 3. Logic failure

Slide 118

Slide 118 text

౴͑ 1. Recoverable error 2. Universal error 3. Logic failure

Slide 119

Slide 119 text

౴͑ 1. Recoverable error 2. Universal error 3. Logic failure

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

// ϥΠϒϥϦϨΠϠʔ func loadGame() -> Game? { ... } // ΞϓϦϨΠϠʔ func ... { guard let game = loadGame() else { fatalError() } // game Λ࢖͏ίʔυ }

Slide 122

Slide 122 text

Q5. ը૾ͷੜ੒

Slide 123

Slide 123 text

let image = Image( width: width, height: height, pixels: pixels)

Slide 124

Slide 124 text

1. Simple domain error struct Image { init?(width: Int, height: Int, pixels: [Pixel]) { guard width >= 0 else { return nil } guard height >= 0 else { return nil } guard pixels.count == width * height else { return nil } ... } }

Slide 125

Slide 125 text

2. Universal error struct Image { init(width: Int, height: Int, pixels: [Pixel]) { guard width >= 0 else { return fatalError() } guard height >= 0 else { return fatalError() } guard pixels.count == width * height else { return fatalError() } ... } }

Slide 126

Slide 126 text

3. Logic failure struct Image { init(width: Int, height: Int, pixels: [Pixel]) { precondition(width >= 0) precondition(height >= 0) precondition(pixels.count == width) ... } }

Slide 127

Slide 127 text

γϯΩϯάλΠϜ 1. Simple domain error 2. Universal error 3. Logic failure

Slide 128

Slide 128 text

౴͑ 1. Simple domain error 2. Universal error 3. Logic failure

Slide 129

Slide 129 text

౴͑ 1. Simple domain error 2. Universal error 3. Logic failure

Slide 130

Slide 130 text

let a = [2, 3, 5] a.prefix(-1) // Logic failure

Slide 131

Slide 131 text

print(3...2) // Logic failure