Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
phantom type
Search
to4iki
May 21, 2017
Programming
160
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
200
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.8k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
5.9k
ビューインプレッションの計測方法
to4iki
1
1.2k
秘伝の `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
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
220
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
750
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
golang-bob.pdf
iwatsukayura
0
130
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
0
470
Domain-Driven Transformation
hschwentner
2
2.3k
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
28
32k
すこし踏み込む CancellationToken
htkym
2
1.7k
FactoryBotをやめればRSpecは速くなるのか?
yuta_onishi_97
0
410
ChromeでローカルLLM
ryu1013
0
140
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
0
1.3k
Featured
See All Featured
First, design no harm
axbom
PRO
2
1.3k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
600
Build your cross-platform service in a week with App Engine
jlugia
234
19k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
930
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
410
How STYLIGHT went responsive
nonsquared
100
6.3k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
850
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?