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
1人で挑むSwiftコンパイラ 〜型システム入門編〜
s_shimotori
0
320
英語
s_shimotori
1
330
Parallel Socket Communication in Swift
s_shimotori
1
590
UIColor Anatomy
s_shimotori
1
2.2k
UIColor Cluster
s_shimotori
1
640
iOSDC Japan 2019 60 mins for color
s_shimotori
5
3.6k
wwdc19
s_shimotori
2
320
SE-0246 Generic Math(s) Functions
s_shimotori
2
2.4k
Introduction to Font and Typesetting
s_shimotori
4
3.7k
Other Decks in Programming
See All in Programming
Rubyのobject_id
qnighy
6
1.3k
Web技術を駆使してユーザーの画面を「録画」する
yukukotani
13
6.4k
Regular Expressions, REXML, Automata Learning
makenowjust
0
190
労務ドメインを快適に開発する方法 / How to Comfortably Develop in the Labor Domain
yuki21
1
250
Wallet API, Verifier APIで実現するIDカード on iPhoneの世界
shitamori1272
1
360
ゲームボーイアドバンスでSwiftを動かそう
k_koheyi
0
540
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
120
Ebitengineの1vs1ゲーム WebRTCの活用
ponyo877
0
350
開発を加速する共有Swift Package実践
elmetal
PRO
0
350
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
440
Swiftコードバトル必勝法
toshi0383
0
150
null or undefined
susisu
22
5.9k
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
334
56k
Practical Orchestrator
shlominoach
185
10k
What's in a price? How to price your products and services
michaelherold
242
11k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
363
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
27
8.9k
We Have a Design System, Now What?
morganepeng
48
7.1k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
KATA
mclloyd
27
13k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
157
15k
Docker and Python
trallard
39
3k
Raft: Consensus for Rubyists
vanstee
135
6.5k
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ͷܧঝͰදݱ͞Ε͍ͯΔ͕ɺ ϧʔϧʹଇ͍ͬͯΔ͔Ͳ͏͔ݟͯ͘Εͳ͍
ਖ਼࣮͘͠͠·͠ΐ͏ʂ ཧΛݴ͑ͦͷςετཉ͘͠ͳ͍ʁ