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
520
英語
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
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
EventSourcingの理想と現実
wenas
6
2.3k
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Jakarta EE meets AI
ivargrimstad
0
530
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Outline View in SwiftUI
1024jp
1
330
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
初めてDefinitelyTypedにPRを出した話
syumai
0
400
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
Amazon Qを使ってIaCを触ろう!
maruto
0
400
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
A Philosophy of Restraint
colly
203
16k
The Invisible Side of Design
smashingmag
298
50k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Faster Mobile Websites
deanohume
305
30k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Site-Speed That Sticks
csswizardry
0
23
Designing for humans not robots
tammielis
250
25k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
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ͷܧঝͰදݱ͞Ε͍ͯΔ͕ɺ ϧʔϧʹଇ͍ͬͯΔ͔Ͳ͏͔ݟͯ͘Εͳ͍
ਖ਼࣮͘͠͠·͠ΐ͏ʂ ཧΛݴ͑ͦͷςετཉ͘͠ͳ͍ʁ