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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
81
イマイチなsubscribeをRxぽくする
yaruki00
0
680
気をつけよう!認知バイアス
yaruki00
1
65
iOSDC2019トークピックアップ
yaruki00
0
53
スクラム完全理解
yaruki00
0
65
小さなチームでも高品質なモバイルアプリを作るためのテスト戦略
yaruki00
0
78
ここがダメだよ🍎さん!
yaruki00
0
45
Meeプロジェクト紹介
yaruki00
0
70
iOSDCまとめ
yaruki00
0
45
Other Decks in Programming
See All in Programming
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.8k
ITヒヤリハットを整理してみた ~ライフサイクルと原因から考える再発防止策~
koukimiura
1
110
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
7
1.9k
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
580
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
270
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
210
継続モナドとリアクティブプログラミング
yukikurage
3
610
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
450
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
360
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
210
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
220
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
170
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Design in an AI World
tapps
1
260
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
360
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
Color Theory Basics | Prateek | Gurzu
gurzu
0
390
Fireside Chat
paigeccino
42
4k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Site-Speed That Sticks
csswizardry
13
1.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
410
Making the Leap to Tech Lead
cromwellryan
135
10k
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ͱ͍͏Ṗͷղܾࡦ ͦΜͳʹίʔυ͘ͳΒͳͯ͘൵͍͠