Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Equatable, Comparable and Hashable
Search
Shigure Shimotori
August 08, 2017
Programming
0
1.4k
Equatable, Comparable and Hashable
Shigure Shimotori
August 08, 2017
Tweet
Share
More Decks by Shigure Shimotori
See All by Shigure Shimotori
Synchronizationを支える技術
s_shimotori
1
160
1人で挑むSwiftコンパイラ 〜型システム入門編〜
s_shimotori
0
530
英語
s_shimotori
1
470
Parallel Socket Communication in Swift
s_shimotori
1
650
UIColor Anatomy
s_shimotori
1
2.3k
UIColor Cluster
s_shimotori
1
690
iOSDC Japan 2019 60 mins for color
s_shimotori
5
3.7k
wwdc19
s_shimotori
2
330
SE-0246 Generic Math(s) Functions
s_shimotori
2
2.4k
Other Decks in Programming
See All in Programming
CSC509 Lecture 13
javiergs
PRO
0
110
flutterkaigi_2024.pdf
kyoheig3
0
150
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
960
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Navigating Team Friction
lara
183
14k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Building Applications with DynamoDB
mza
90
6.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Being A Developer After 40
akosma
87
590k
Ruby is Unlike a Banana
tanoku
97
11k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
It's Worth the Effort
3n
183
27k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Transcript
Equatable / Comparable / Hashable Λશʹཧղ͢Δ @S_Shimotori Otemachi.swift x Kyobashi.swift
#01
ͩΕ • @S_Shimotori·ͨ@S_Shimotori_pub • ֶੜ • DroidKaigi2016-2018ӡӦϝϯόʔ
iOSDCʹొஃ͠·͢ • #1349ʮARC vs. GCʁARC in GCʁʯ • 5ͰGCͷΛ͠·͢ •
ݫ͍͠ • Reject͞Εͨͭదʹڙཆ͢ΔͭΓ • ςετ • ԋࢉࢠ • Ͳ͜Ͱʁ
Equatable Comparable Hashable
͓͞Β͍ • Equatable • ͍͔͠Ͳ͏͔ൺֱͰ͖Δ • Comparable • ؔԋࢉࢠͰൺֱͰ͖Δ •
͍͍ͪͪൺֱ༻ͷؔΛ༻ҙ͠ͳͯ͘ιʔτͰ͖Δ • Hashable • ϋογϡΛ࡞ΕΔ • Map/DictionaryͷΩʔʹͳΕΔ • JavaComparableͷΈinterfaceͰΓclass Objectʹੜ͑ͯΔ • Pythonduck typing
SwiftͰ:
EquatableͱΏ͔͍ͳؒͨͪ(Ұ෦লུ) &RVBUBCMF $PNQBSBCMF )BTIBCMF /VNFSJD 4FU"MHFCSB 4USJEFBCMF 4USJOH1SPUPDPM 3FGFSFODF$POWFSUJCMF Foundationͷࢀরܕଓ͘ɻ
String͕ଓ͘ɻ 1࣍ݩͷ࿈ଓతͳɻ UnsafePointer͕ଓ͘ɻ ೋ߲ࢉज़ԋࢉɻ Set͕ଓ͘ɻ • ͬͱਓؾͳProtocolͷ1ͭ • ͳʹΑΓଘࡏײ͕େ͖͍ɺେ͖͗͢Δ • Equatable.swiftʹͪΌ͔ͬΓಉҰఆͷ࣮͍Δ
protocol Equatable
ͱΓ͋͑ͣ == Λ࣮͢ΕOKʂ ❌
protocol Equatableͷཁ݅ • ସՄೳੑ • ࣹੑ • ରশੑ • ਪҠੑ
• a == b ͕ਅͳΒ not a != b ͕ਅ • ͜ͷ͏ͪEquatable͔ΒϑΥϩʔ͕ೖΔͷ࠷ޙ͚ͩʂ • ҙਤతʹΦʔόʔϥΠυ͠ͳ͍ݶΓ==ͷٯͷ݁ՌΛฦ͢
Optional<T>ͱEquatableͷؔ • OptionalEquatableͰͳ͍ • Ͱis Equatableͷ݁Ռதʹґଘ͢Δ • ͰผͰԋࢉࢠ͕࣮͞Ε͍ͯΔͷͰnilͱͷൺֱՄೳ
protocol Comparable
దʹ < Λ࣮͓͚ͯ͠ ͳΜͱ͔ͳΔͭ ❌
protocol Comparableͷׂͱཁ݅ • ܕʹࣗવॱং͚Λ༩͑Δ • σϑΥϧτͷιʔτํ๏ΛܾΊΔͱ͍͏͜ͱ • SwiftؔԋࢉࢠͰදݱ͍ͯ͠ΔͷͰEquatable͕ඞਢ • ࡶʹ࣮͠ͳ͍ݶΓEquatableͱໃ६͠ͳ͍
• ࡶʹ࣮͠ͳ͍ݶΓ < = > ͷͲΕ͔1͚͕ͭͩtrueʹͳΔ • strict total orderingͱݺΕΔੑ࣭Λຬͨ͞ͳ͚ΕͳΒͳ͍ • ʮ͖ͬͪΓϧʔϧΛܾΊͯશһҰྻʹฒͳ͍ͱͩΊʯ • ුಈখͳͲͰྫ֎͕ೝΊΒΕΔ߹͕͋Δ
sort2छྨ • Ҿͳ͠ͷsort / sorted • ࣗવॱং͚ʹैͬͯιʔτ͢Δ • ͭ·Γཁૉ͕ComparableͰͳ͚ΕͳΒͳ͍ •
ҾΛऔΔsort / sorted • ରίϨΫγϣϯͷΈͰ௨༻͢Δॱং͚Ͱιʔτ͢Δ • ཁૉ͕ComparableͰͳ͍͍ͯ͘ • strict weak orderingͳॱং͚͕ڐ͞ΕΔ • Comparableͱͷҧ͍ൺֱෆೳ͕ڐ͞ΕΔ͜ͱ • ͲͪΒ࠷ޙॱং͚ʹैͬͯΠϯτϩιʔτ͞ΕΔ
protocol Hashable
ͱΓ͋͑ͣ ϋογϡΛग़͓͚ͯ͠OKʂ ❌
protocol Hashable • ϋογϡΛ࣮͢Δͱ͖ͷΑ͋͘Δҙ • ޓ͍ʹಉͳΒϋογϡ͘͠ͳ͚ΕͳΒͳ͍ • ٯཱ͠ͳͯ͘Α͔ͬͨΓ͢Δ • SwiftͰEquatableඞਢͳͷͰ࣮ΛΕΔ͜ͱͳ͍ͣ
• ϧʔϧʹଇͬͯͪΌΜͱ࣮͢Δ͜ͱʂ
Ҏ্ʂ
͓·͚
assertEqual
assertEqualʹݴޠͷݸੑ͕ग़Δ • Javaʹࢀরܕ༻ͱϓϦϛςΟϒܕ༻͕ͦΕͧΕ༻ҙ͞Ε͍ͯΔ • Pythonʹ1छྨ͔͠ͳ͍ • Objective-CΦϒδΣΫτ༻ͱεΧϥ༻͕༻ҙ͞Ε͍ͯΔ • SwiftNonNull༻ͱNullable༻͕༻ҙ͞Ε͍ͯΔ •
ࢀরܕͱܕͰͷ۠ผͰͳ͍ • ͜ͷ2ͭ΄ͱΜͲҧ͍͕ͳ͍ɺ࣮࣭1छྨɻ
·ͱΊ
·ͱΊ • Equatable / Comparable / Hashableʹϧʔϧ͕͋Δ • ޓ͍ͷؔੑprotocolͷܧঝͰදݱ͞Ε͍ͯΔ͕ɺ ϧʔϧʹଇ͍ͬͯΔ͔Ͳ͏͔ݟͯ͘Εͳ͍
ਖ਼࣮͘͠͠·͠ΐ͏ʂ ཧΛݴ͑ͦͷςετཉ͘͠ͳ͍ʁ