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
phantom type
Search
to4iki
May 21, 2017
Programming
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
phantom type
👻
to4iki
May 21, 2017
More Decks by to4iki
See All by to4iki
Claude Code の活用事例
to4iki
0
180
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.7k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
5.7k
ビューインプレッションの計測方法
to4iki
1
1.1k
秘伝の `gitconfig`
to4iki
1
470
Abema iOS Architecture
to4iki
12
3.6k
timetable-bot
to4iki
0
15k
BLoC Pattern Introduction with Swift
to4iki
2
1.4k
nel
to4iki
0
200
Other Decks in Programming
See All in Programming
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
280
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.5k
LLMによるContent Moderationの本番運用の裏側と品質担保への挑戦
suikabar
3
800
symfony/aiとlaravel/boost
77web
0
110
Datadog LLM Observabilityで実現する 安全なLLM Usage 管理
3150
0
130
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
370
Oxlintのカスタムルールの現況
syumai
6
1.2k
Performance Engineering for Everyone
elenatanasoiu
0
240
Contextとはなにか
chiroruxx
1
380
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.7k
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
280
Featured
See All Featured
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
170
Accessibility Awareness
sabderemane
1
150
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Tell your own story through comics
letsgokoyo
1
980
Discover your Explorer Soul
emna__ayadi
2
1.2k
4 Signs Your Business is Dying
shpigford
187
22k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
170
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.4k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
GitHub's CSS Performance
jonrohan
1033
470k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
350
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
160
Transcript
Phantom Type shinjyuku.lt #8 @to4iki
People • @to4iki • GANMA!ͱ͍͏ອըͷαʔϏεΛ࡞ͬͯ ·͢ • ࠷ۙͷϑΝγϦςʔγϣϯʹڵຯ͕͋Δ • ͖ͳ৯Ͱ͢
• ੴ֞ౡʹߦͧ͘ʂ
None
Γ·͢ https://builderscon.io/tokyo/2017
ϘϥϯςΟΞελοϑ ืू͍ͯ͠ΔΑʂ
Agenda • Phantom Type ͱ • ࣮༻ྫ
Phantom Type
Phantom Typeͱ#1 • ܕݕࠪΛར༻ͨ͠σβΠϯύλʔϯ • HaskellScalaք۾ͳͲͰطʹྺ࢙ͷ͋Δύ λʔϯ
Phantom Typeͱ#2 • Πϯελϯεͷঢ়ଶΛͰͳ͘ܕύϥϝʔλͱ͠ ͯ࣋ͭ͜ͱͰɺঢ়ଶνΣοΫΛ࣮ߦ࣌Ͱͳ͘ίϯ ύΠϧ࣌ʹߦ͏ • => ܕͦͷͷʹঢ়ଶΛͯࠐΉ •
ݟ্ͨݱΕͳ੍͍ɺΘΕͳ͍ܕ • => Έ͍ͨͳܕ
݅Λຬͨ͞ͳ͍ͱ ढ͍(ίϯύΠϧΤϥʔ) Λ͔͚Δ
݅Λຬͨ͢ͱ ͠ফ͑ڈΔ
None
example.
ঢ়ଶΛઃܭ͢Δ // ૢ࡞ঢ়ଶΛදݱ͢ΔI/F protocol OperationState {} // ૢ࡞ঢ়ଶຖͷΫϥεఆٛ class Init:
OperationState {} class Prepared: OperationState {}
ঢ়ଶຖͷػೳΛઃܭ͢Δ // ܕύϥϝʔλͰঢ়ଶΛද͢ class HogeOperation<State: OperationState> {} // ঢ়ଶຖʹػೳΛઃܭ͢Δ extension
HogeOperation where State == Init { func prepared() -> HogeOperation<Prepared> { return HogeOperation<Prepared>() } } extension HogeOperation where State == Prepared { func execute() { print("do something") } }
ঢ়ଶʹԠͯ͡ɺػೳ͕ΓସΘΔ // ४උঢ়ଶ let operation = HogeOperation<Init>() operation.execute() // compile
error // ४උྃঢ়ଶ let preparedOperation = operation.prepared() preparedOperation.execute() preparedOperation.prepared() // compile error
࣮༻ྫ
ࣄલ࣮ߦΛڧ੍͢ΔBuilder // builderͷঢ়ଶ protocol BuilderState {} class Buildable: BuilderState {}
class Unbuildable: BuilderState {} class UserBuilder<Name: BuilderState, Age: BuilderState> { fileprivate let name: String fileprivate let age: UInt private init(name: String, age: UInt) { self.name = name self.age = age } static var builder: UserBuilder<Unbuildable, Unbuildable> { return UserBuilder<Unbuildable, Unbuildable>(name: "", age: 0) } func set(name: String) -> UserBuilder<Buildable, Age> { return UserBuilder<Buildable, Age>(name: name, age: age) } func set(age: UInt) -> UserBuilder<Name, Buildable> { return UserBuilder<Name, Buildable>(name: name, age: age) } }
ࣄલ࣮ߦΛڧ੍͢ΔBuilder struct User { let name: String let age: UInt
} // NameͱAge͕Buildableͷ࣌ extension UserBuilder where Name: Buildable, Age: Buildable { funsc build() -> User { return User(name: name, age: age) } }
ࣄલ࣮ߦΛڧ੍͢ΔBuilder let builder = UserBuilder<Unbuildable, Unbuildable>.builder builder.set(name: "hoge").build() // compile
error builder.set(age: 10).build() // compile error let user = builder .set(name: "hoge") .set(age: 10) .build()
Conclusion • PhantomTypeΛ͏͜ͱͰɺܕύϥϝʔλʔ ʹΑΓΦϒδΣΫτͷৼΔ͍ʹ੍Λ༩͑ Δ͜ͱ͕Ͱ͖Δ • ܕύϥϝʔλʔ͕ҧ࣮͑ଶҟͳΔ • ίϯύΠϥʹΑΔঢ়ଶνΣοΫͷݕࠪΛߦ͏ ͜ͱ͕Ͱ͖Δ
See also • http://qiita.com/taketo1024/items/71e3272211f08d7e0cde • https://www.slideshare.net/tomohirokumagai54/phantom-kbkztech • https://www.objc.io/blog/2014/12/29/functional-snippet-13-phantom- types/
Any Questions?