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
Swift Generic Protocol
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Yuta Kawabe
December 06, 2017
Programming
44
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swift Generic Protocol
Yuta Kawabe
December 06, 2017
More Decks by Yuta Kawabe
See All by Yuta Kawabe
年末年始読んだ本LT
yaruki00
0
87
イマイチなsubscribeをRxぽくする
yaruki00
0
680
気をつけよう!認知バイアス
yaruki00
1
65
iOSDC2019トークピックアップ
yaruki00
0
56
スクラム完全理解
yaruki00
0
65
小さなチームでも高品質なモバイルアプリを作るためのテスト戦略
yaruki00
0
79
ここがダメだよ🍎さん!
yaruki00
0
45
Meeプロジェクト紹介
yaruki00
0
75
iOSDCまとめ
yaruki00
0
48
Other Decks in Programming
See All in Programming
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
32
20k
リアルな遅延を測る仕様
kota_yata
1
130
思考垂れ流し開発 ~音声入力 × AIエージェント × 開発ハーネスによる試行錯誤~
npostring
0
390
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
1
820
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
460
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
760
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
160
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
110
「人を評価する AI」の設計と実装
ryoyanara
0
260
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
120
バグを直したら useEffect が消えた
colorful12
3
760
Featured
See All Featured
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
410
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
480
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
350
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
670
Claude Code のすすめ
schroneko
67
230k
Faster Mobile Websites
deanohume
310
32k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
310
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
Google's AI Overviews - The New Search
badams
0
1.6k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Transcript
Swift Generic Protocol 2017/12/6(ਫ) ΤϯδχΞษڧձ ล ༟ଠ
͡Ίʹ ςʔϚ͕LTͰѻ͏ʹॏ͍ ͦͷׂʹࢿྉ͕ࡶ ໌Β͔ʹ༨ܭͳͱ͜Ζʹ࿑ྗΛׂ͍͍ͯΔ ͝ΊΜͶʂ
Ϟνϕʔγϣϯ Wireframe(Routerϓϩτίϧ)Λڞ௨Խ͍ͨ͠ ֤ContractʹҎԼͷΑ͏ͳίʔυ HogeViewͷ෦֤ViewΛฦ͍ͨ͠ protocol HogeWireframe: class { weak var
view: HogeView? { get set } static func assembleModule() -> HogeView } Generics͡Όʂ
Genericͱ͍͑ classstruct, funcʹtype parameterΛ͚ͭΔ class SomeClass<T: Int> { var someProperty:
[T] = [] func someFunction<S>(arg0: T, arg1: S) { print(arg0, arg1) } } let c = SomeClass<Int>() // func ͷ߹type parameterΛ໌ࣔ͠ͳ͍ c.someFunction(arg0: 0, arg1: "hello")
protocolಉ༷ʹ…ʁ ͖ͬͱ͜Μͳײ͡Ͱ͠ΐʁ protocol SomeProtocol<T: Int> { var someProperty: [T] =
[] func someFunction<S>(arg0: T, arg1: S) } var p: SomeProtocol<Int>
None
classͷΑ͏ʹͰ͖ͳ͍ protocolͷએݴʹtype parameter͑ͳ͍ ͳΜͰ͔Α͘Θ͔Βͳ͍ Ͱgenericʹ͍ͨ͠͡ΌΜʁ
͡Ό͋Ͳ͏͢Μͷ͞ SelfΛ͏ protocolʹద߹͢ΔΫϥεͳΒ͜ΕͰ associatedtypeΛ͏ ͦͷଞͷΫϥεͪ͜Β
Self ద߹͢ΔΫϥεʹͳΔ classͰ͑Δ(ܧঝ͢Δͱ͖ͱ͔) protocol HasSelfProtocol { func returnSelf() -> Self
} class AdoptHasSelfClass: HasSelfProtocol { func returnSelf() -> Self { return self } } var hasSelfProtocol: HasSelfProtocol = AdoptHasSelfClass()
associatedtype ҙͷܕΛએݴͰ͖Δ protocol AssociatedTypeProtocol { associatedtype T var someProperty: T
{ set get } func someFunction() -> T } class AdoptAssociatedTypeClass: AssociatedTypeProtocol { typealias T = Int // ແͯ͘Α͍ var someProperty: Int = 0 func someFunction() -> Int { return someProperty } }
type parameterͱԿ͕ҧ͏ͷ͔ɾɾɾ
ͦΕͱ͔͘ Generic ProtocolΛ࡞Δ͜ͱ͕Ͱ͖ͨʂ ޙ͜ΕΛϓϩύςΟͷܕʹͯ͠ɾɾɾ var associatedTypeProtocol: AssociatedTypeProtocol = AdoptAssociatedTypeClass()
িܸͷ৽ࣄ࣮ Generic ProtocolϓϩύςΟͷܕʹͰ͖ͳ͍ ύϑΥʔϚϯεతͳཧ༝Ͱ(ਪଌ) https://qiita.com/omochimetaru/items/b41e7699ea25a324aefa ٕज़తʹͰ͖ͳ͍͜ͱͳ͍Β͍͠
Type Erasureͱ͍͏ղܾࡦ ϓϩτίϧΛ࣮ࡍͷܕʹద༻ struct AnyAssociatedType<A: AssociatedTypeProtocol>: AssociatedTypeProtocol { typealias T
= A.T var someProperty: T private let _someFunction: () -> T func someFunction() -> T { return _someFunction() } init<Inner: AssociatedTypeProtocol>(_ inner: Inner) where T == Inner.T { self.someProperty = inner.someProperty self._someFunction = inner.someFunction } } var associatedTypeProtocol: AnyAssociatedType<AdoptAssociatedTypeClass> = AnyAssociatedType(AdoptAssociatedTypeClass())
݁ہͲ͏ͬͨͷʁ ͳΜ͔Ϋϥε࡞Γ·ͨ͠ protocol BaseViewWireframe: class { associatedtype SomeView: UIView weak
var view: SomeView? { get set } static func assembleModule(vc: UIViewController) -> SomeView } class HogeWireframe: BaseViewWireframe { weak var view: HogeViewImpl? class func assembleModule(vc: UIViewController) -> HogeViewImpl { return HogeViewImpl() } }
·ͱΊ SwiftͷGeneric Protocol੍Ͱ͍ʹ͍͘ type parameter͑ͳ͍ ϓϩύςΟͷܕʹͰ͖ͳ͍ Type Erasureͱ͍͏Ṗͷղܾࡦ ͦΜͳʹίʔυ͘ͳΒͳͯ͘൵͍͠