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
550
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
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
Recoilを剥がしている話
kirik
5
6.8k
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
280
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
390
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
280
testcontainers のススメ
sgash708
1
120
Effective Signals in Angular 19+: Rules and Helpers
manfredsteyer
PRO
0
110
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
2
100
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
150
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
Featured
See All Featured
RailsConf 2023
tenderlove
29
940
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Code Reviewing Like a Champion
maltzj
520
39k
Unsuck your backbone
ammeep
669
57k
Six Lessons from altMBA
skipperchong
27
3.5k
Making the Leap to Tech Lead
cromwellryan
133
9k
Navigating Team Friction
lara
183
15k
Code Review Best Practice
trishagee
65
17k
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ͷܧঝͰදݱ͞Ε͍ͯΔ͕ɺ ϧʔϧʹଇ͍ͬͯΔ͔Ͳ͏͔ݟͯ͘Εͳ͍
ਖ਼࣮͘͠͠·͠ΐ͏ʂ ཧΛݴ͑ͦͷςετཉ͘͠ͳ͍ʁ