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
180
1人で挑むSwiftコンパイラ 〜型システム入門編〜
s_shimotori
0
620
英語
s_shimotori
1
540
Parallel Socket Communication in Swift
s_shimotori
1
680
UIColor Anatomy
s_shimotori
1
2.4k
UIColor Cluster
s_shimotori
1
700
iOSDC Japan 2019 60 mins for color
s_shimotori
5
3.7k
wwdc19
s_shimotori
2
340
SE-0246 Generic Math(s) Functions
s_shimotori
2
2.4k
Other Decks in Programming
See All in Programming
Jakarta EE meets AI
ivargrimstad
0
240
From Translations to Multi Dimension Entities
alexanderschranz
2
130
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
1
150
Beyond ORM
77web
3
430
rails statsで大解剖 🔍 “B/43流” のRailsの育て方を歴史とともに振り返ります
shoheimitani
2
930
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
暇に任せてProxmoxコンソール 作ってみました
karugamo
1
720
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
MCP with Cloudflare Workers
yusukebe
2
220
Featured
See All Featured
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Speed Design
sergeychernyshev
25
670
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
GraphQLとの向き合い方2022年版
quramy
44
13k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Six Lessons from altMBA
skipperchong
27
3.5k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
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ͷܧঝͰදݱ͞Ε͍ͯΔ͕ɺ ϧʔϧʹଇ͍ͬͯΔ͔Ͳ͏͔ݟͯ͘Εͳ͍
ਖ਼࣮͘͠͠·͠ΐ͏ʂ ཧΛݴ͑ͦͷςετཉ͘͠ͳ͍ʁ