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
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
190
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.8k
ケースに応じた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
大喜利で理解するLLM as a Judge / Understanding LLM-as-a-Judge through Ogiri
rockname
0
150
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
330
The Rails Doctrine Decade
koic
2
170
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
310
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
5
3.5k
AWS Step Functions 大規模並列の壁を越える / jaws-sonic-2026-niigata-step-functions
kasacchiful
PRO
1
490
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
280
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
340
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
160
SREの越境 / SRE Collaboration
y0hgi
2
260
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
620
Security issues being discussed on Web Platforms
petamoriken
0
990
Featured
See All Featured
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
Design in an AI World
tapps
1
320
How to Talk to Developers About Accessibility
jct
2
550
Making the Leap to Tech Lead
cromwellryan
135
10k
My Coaching Mixtape
mlcsv
0
320
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
390
Darren the Foodie - Storyboard
khoart
PRO
4
3.9k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
510
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
240
Producing Creativity
orderedlist
PRO
348
41k
Code Reviewing Like a Champion
maltzj
528
40k
How to Think Like a Performance Engineer
csswizardry
28
2.8k
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?