Slide 1

Slide 1 text

Equatable / Comparable / Hashable Λ׬શʹཧղ͢Δ @S_Shimotori Otemachi.swift x Kyobashi.swift #01

Slide 2

Slide 2 text

ͩΕ • @S_Shimotori·ͨ͸@S_Shimotori_pub • ֶੜ • DroidKaigi2016-2018ӡӦϝϯόʔ

Slide 3

Slide 3 text

iOSDCʹొஃ͠·͢ • #1349ʮARC vs. GCʁARC in GCʁʯ • 5෼ͰGCͷ࿩Λ͠·͢ • ݫ͍͠ • Reject͞Εͨ΍ͭ΋ద౰ʹڙཆ͢Δͭ΋Γ • ςετ • ԋࢉࢠ • Ͳ͜Ͱʁ

Slide 4

Slide 4 text

Equatable Comparable Hashable

Slide 5

Slide 5 text

͓͞Β͍ • Equatable • ౳͍͔͠Ͳ͏͔ൺֱͰ͖Δ • Comparable • ؔ܎ԋࢉࢠͰൺֱͰ͖Δ • ͍͍ͪͪൺֱ༻ͷؔ਺Λ༻ҙ͠ͳͯ͘΋ιʔτͰ͖Δ • Hashable • ϋογϡ஋Λ࡞ΕΔ • Map/DictionaryͷΩʔʹͳΕΔ • Java͸ComparableͷΈinterfaceͰ࢒Γ͸class Objectʹੜ͑ͯΔ • Python͸duck typing

Slide 6

Slide 6 text

SwiftͰ͸:

Slide 7

Slide 7 text

EquatableͱΏ͔͍ͳ஥ؒͨͪ(Ұ෦লུ) &RVBUBCMF $PNQBSBCMF )BTIBCMF /VNFSJD 4FU"MHFCSB 4USJEFBCMF 4USJOH1SPUPDPM 3FGFSFODF$POWFSUJCMF Foundation಺ͷࢀরܕ΁ଓ͘ɻ String͕ଓ͘ɻ 1࣍ݩͷ࿈ଓతͳ஋ɻ ਺஋΍UnsafePointer͕ଓ͘ɻ ೋ߲ࢉज़ԋࢉɻ Set͕ଓ͘ɻ • ΋ͬͱ΋ਓؾͳProtocolͷ1ͭ • ͳʹΑΓଘࡏײ͕େ͖͍ɺେ͖͗͢Δ • Equatable.swiftʹ͸ͪΌ͔ͬΓಉҰ൑ఆͷ࣮૷΋͍Δ

Slide 8

Slide 8 text

protocol Equatable

Slide 9

Slide 9 text

ͱΓ͋͑ͣ == Λ࣮૷͢Ε͹OKʂ ❌

Slide 10

Slide 10 text

protocol Equatableͷཁ݅ • ୅ସՄೳੑ • ൓ࣹੑ • ରশੑ • ਪҠੑ • a == b ͕ਅͳΒ not a != b ͕ਅ • ͜ͷ͏ͪEquatable͔ΒϑΥϩʔ͕ೖΔͷ͸࠷ޙ͚ͩʂ • ҙਤతʹΦʔόʔϥΠυ͠ͳ͍ݶΓ==ͷٯͷ݁ՌΛฦ͢

Slide 11

Slide 11 text

OptionalͱEquatableͷؔ܎ • Optional͸EquatableͰ͸ͳ͍ • Ͱ΋is Equatableͷ݁Ռ͸த਎ʹґଘ͢Δ • Ͱ΋ผͰԋࢉࢠ͕࣮૷͞Ε͍ͯΔͷͰnilͱͷൺֱ͸Մೳ

Slide 12

Slide 12 text

protocol Comparable

Slide 13

Slide 13 text

ద౰ʹ < Λ࣮૷͓͚ͯ͠͹ ͳΜͱ͔ͳΔ΍ͭ ❌

Slide 14

Slide 14 text

protocol Comparableͷ໾ׂͱཁ݅ • ܕʹࣗવॱং෇͚Λ༩͑Δ • σϑΥϧτͷιʔτํ๏ΛܾΊΔͱ͍͏͜ͱ • Swift͸ؔ܎ԋࢉࢠͰදݱ͍ͯ͠ΔͷͰEquatable͕ඞਢ • ࡶʹ࣮૷͠ͳ͍ݶΓ͸Equatableͱໃ६͠ͳ͍ • ࡶʹ࣮૷͠ͳ͍ݶΓ͸ < = > ͷͲΕ͔1͚͕ͭͩtrueʹͳΔ • strict total orderingͱݺ͹ΕΔੑ࣭Λຬͨ͞ͳ͚Ε͹ͳΒͳ͍ • ʮ͖ͬͪΓϧʔϧΛܾΊͯશһҰྻʹฒ΂ͳ͍ͱͩΊʯ • ුಈখ਺఺਺ͳͲͰྫ֎͕ೝΊΒΕΔ৔߹͕͋Δ

Slide 15

Slide 15 text

sort͸2छྨ • Ҿ਺ͳ͠ͷsort / sorted • ࣗવॱং෇͚ʹैͬͯιʔτ͢Δ • ͭ·Γཁૉ͕ComparableͰͳ͚Ε͹ͳΒͳ͍ • Ҿ਺ΛऔΔsort / sorted • ର৅ίϨΫγϣϯ಺ͷΈͰ௨༻͢Δॱং෇͚Ͱιʔτ͢Δ • ཁૉ͕ComparableͰͳͯ͘΋͍͍ • strict weak orderingͳॱং෇͚͕ڐ͞ΕΔ • Comparableͱͷҧ͍͸ൺֱෆೳ͕ڐ͞ΕΔ͜ͱ • ͲͪΒ΋࠷ޙ͸ॱং෇͚ʹैͬͯΠϯτϩιʔτ͞ΕΔ

Slide 16

Slide 16 text

protocol Hashable

Slide 17

Slide 17 text

ͱΓ͋͑ͣ ϋογϡ஋Λग़͓͚ͯ͠͹OKʂ ❌

Slide 18

Slide 18 text

protocol Hashable • ϋογϡΛ࣮૷͢Δͱ͖ͷΑ͋͘Δ஫ҙ • ޓ͍ʹಉ஋ͳΒϋογϡ஋΋౳͘͠ͳ͚Ε͹ͳΒͳ͍ • ٯ͸੒ཱ͠ͳͯ͘΋Α͔ͬͨΓ͢Δ • SwiftͰ͸EquatableඞਢͳͷͰ࣮૷Λ๨ΕΔ͜ͱ͸ͳ͍͸ͣ • ϧʔϧʹଇͬͯͪΌΜͱ࣮૷͢Δ͜ͱʂ

Slide 19

Slide 19 text

Ҏ্ʂ

Slide 20

Slide 20 text

͓·͚

Slide 21

Slide 21 text

assertEqual

Slide 22

Slide 22 text

assertEqualʹ͸ݴޠͷݸੑ͕ग़Δ • Javaʹ͸ࢀরܕ༻ͱϓϦϛςΟϒܕ༻͕ͦΕͧΕ༻ҙ͞Ε͍ͯΔ • Pythonʹ͸1छྨ͔͠ͳ͍ • Objective-C͸ΦϒδΣΫτ༻ͱεΧϥ༻͕༻ҙ͞Ε͍ͯΔ • Swift͸NonNull༻ͱNullable༻͕༻ҙ͞Ε͍ͯΔ • ࢀরܕͱ஋ܕͰͷ۠ผͰ͸ͳ͍ • ͜ͷ2ͭ΋΄ͱΜͲҧ͍͕ͳ͍ɺ࣮࣭1छྨɻ

Slide 23

Slide 23 text

·ͱΊ

Slide 24

Slide 24 text

·ͱΊ • Equatable / Comparable / Hashableʹ͸ϧʔϧ͕͋Δ • ޓ͍ͷؔ܎ੑ͸protocolͷܧঝͰදݱ͞Ε͍ͯΔ͕ɺ ϧʔϧʹଇ͍ͬͯΔ͔Ͳ͏͔͸ݟͯ͘Εͳ͍ ਖ਼࣮͘͠૷͠·͠ΐ͏ʂ ཧ૝Λݴ͑͹ͦͷςετ΋ཉ͘͠ͳ͍ʁ