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.8k
ビューインプレッションの計測方法
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
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
430
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
240
freee が目指す データ マネジメント戦略 AI-Ready 時代を支える 攻めのガバナンスとは
freee
PRO
0
480
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
500
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.7k
不幸な GC
chencmd
0
730
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
720
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
150
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
1
2k
Go 1.27 における memory allocation の高速化
andpad
0
320
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
510
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.8k
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
30 Presentation Tips
portentint
PRO
1
380
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
220
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
500
How to build a perfect <img>
jonoalderson
1
5.9k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
580
Documentation Writing (for coders)
carmenintech
77
5.5k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
エンジニアに許された特別な時間の終わり
watany
108
250k
Site-Speed That Sticks
csswizardry
13
1.5k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.4k
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?