Slide 1

Slide 1 text

Swift Album
 ver.4.1

Slide 2

Slide 2 text

Swift

Slide 3

Slide 3 text

apple/swift

Slide 4

Slide 4 text

What’s happen in Swift4.1??

Slide 5

Slide 5 text

ezura • iOS engineer @ LINE • Advent calendar 2017 • Ͳ͏ͯͦ͠ͷػೳ/࢓༷͸Swiftʹͳ͍ͷʁ • Swift4.1+ @eduraaa

Slide 6

Slide 6 text

ezura • iOS engineer @ LINE • Advent calendar 2017 • Ͳ͏ͯͦ͠ͷػೳ/࢓༷͸Swiftʹͳ͍ͷʁ • Swift4.1+ @eduraaa

Slide 7

Slide 7 text

Topics • `Equatable`/`Hashable`ద߹ʹΑΔ`==`/`hashValue`ͷ҉໧త ࣮૷ • ಛఆͷ৚݅ԼͰͷϓϩτίϧద߹ • ࠶ىతͳϓϩτίϧ੍໿ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ • ϓϩτίϧ಺ͰͷΦʔφʔγοϓͷએݴΛ࡟আ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ

Slide 8

Slide 8 text

Topics • `Equatable`/`Hashable`ద߹ʹΑΔ`==`/`hashValue`ͷ҉໧త ࣮૷ • ಛఆͷ৚݅ԼͰͷϓϩτίϧద߹ • ࠶ىతͳϓϩτίϧ੍໿ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ • ϓϩτίϧ಺ͰͷΦʔφʔγοϓͷએݴΛ࡟আ • Unsafe[Mutable][Raw][Buffer]PointerͷAPIઃܭͷվળ

Slide 9

Slide 9 text

`==`/`hashValue`ͷ
 ҉໧త࣮૷

Slide 10

Slide 10 text

struct CustomType { let v1: String let v2: String let v3: String } // CustomType instance == CustomType instance customInstance1 == customInstance2

Slide 11

Slide 11 text

struct CustomType { let v1: String let v2: String let v3: String } // CustomType instance == CustomType instance customInstance1 == customInstance2 error: binary operator '==' cannot be applied to two 'CustomType' operands

Slide 12

Slide 12 text

struct CustomType: Equatable { let v1: String let v2: String let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } } ໌ࣔతʹ࣮૷͢Δඞཁ͕͋ͬͨ

Slide 13

Slide 13 text

੒௕

Slide 14

Slide 14 text

struct CustomType: Equatable { let v1: String let v2: String let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } }

Slide 15

Slide 15 text

struct CustomType: Equatable { let v1: String let v2: String let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } }

Slide 16

Slide 16 text

struct CustomType: Equatable { let v1: String let v2: String let v3: String static func ==(lhs: CustomType, rhs: CustomType) -> Bool { return lhs.v1 == rhs.v1 && lhs.v2 == rhs.v2 && lhs.v3 == rhs.v3 } } ৚݅: શͯ `Equatable` ʹద߹ ৚݅: એݴ෦Ͱ `Equatable` ʹద߹

Slide 17

Slide 17 text

enum Token: Equatable { case string(String) case number(Int) case lparen case rparen case custom(CustomType) static func == (lhs: Token, rhs: Token) -> Bool { switch (lhs, rhs) { case (.string(let lhsString), .string(let rhsString)): return lhsString == rhsString case (.number(let lhsNumber), .number(let rhsNumber)): return lhsNumber == rhsNumber case (.custom(let lhsCustom), .custom(let rhsCustom)): return lhsCustom == rhsCustom
 case (.lparen, .lparen), (.rparen, .rparen): return true default: return false } } }

Slide 18

Slide 18 text

enum Token: Equatable { case string(String) case number(Int) case lparen case rparen case custom(CustomType) static func == (lhs: Token, rhs: Token) -> Bool { switch (lhs, rhs) { case (.string(let lhsString), .string(let rhsString)): return lhsString == rhsString case (.number(let lhsNumber), .number(let rhsNumber)): return lhsNumber == rhsNumber case (.lparen, .lparen), (.rparen, .rparen): return true case (.custom(let lhsCustom), .custom(let rhsCustom)): return lhsCustom == rhsCustom default: return false } } } ৚݅: શͯ `Equatable` ʹద߹ ৚݅: એݴ෦Ͱ `Equatable` ʹద߹

Slide 19

Slide 19 text

conditional conformance

Slide 20

Slide 20 text

protocol P {} struct S {} extension S: P where T: P {} ͱ͋Δ৚݅Λຬ͍ͨͯ͠Δ৔߹͚ͩ
 ܕʹ Protocol ͕ద߹͞ΕΔ

Slide 21

Slide 21 text

extension Array: Equatable where Element: Equatable { static func ==(lhs: Array, rhs: Array) -> Bool {
 … } } `Element` ͕ `Equatable` ͷͱ͖
 ࣗ਎͕ `Equatable` ʹద߹

Slide 22

Slide 22 text

ݱࡏ

Slide 23

Slide 23 text

extension Array: Equatable where Element: Equatable { static func ==(lhs: Array, rhs: Array) -> Bool {
 … } } ॻ͚ͳ͍ʂ

Slide 24

Slide 24 text

Array == Array 
 [1, 2, 3] == [1, 2, 3]

Slide 25

Slide 25 text

Array> == Array> 
 [[1, 2, 3]] == [[1, 2, 3]]

Slide 26

Slide 26 text

Array> == Array> 
 [[1, 2, 3]] == [[1, 2, 3]]

Slide 27

Slide 27 text

Array> == Array> 
 [[1, 2, 3]] == [[1, 2, 3]] `==` ͰൺֱͰ͖Δ͚ΕͲ
 `Array` ࣗମ͸ `Equatable` Ͱ͸ͳ͍

Slide 28

Slide 28 text

< Conditional conformances

Slide 29

Slide 29 text

extension Array: Equatable where Element: Equatable { static func ==(lhs: Array, rhs: Array) -> Bool {
 … } } `Element` ͕ `Equatable` ͷͱ͖
 ࣗ਎͕ `Equatable` ʹద߹

Slide 30

Slide 30 text

Array> == Array> 
 [[1, 2, 3]] == [[1, 2, 3]] `Array` ࣗମ΋ `Equatable`

Slide 31

Slide 31 text

ৄࡉ͸ͪ͜Β LINE Advent Calendar 2017
 Swift 4.1+
 https://engineering.linecorp.com/ja/blog/detail/227