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
霧の中の代数的エフェクト
funnyycat
1
390
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
150
どこまでゆるくて許されるのか
tk3fftk
0
490
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
220
The Bowling Game- From Imperative to Functional Programming - Part 1
philipschwarz
PRO
0
330
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
140
共通化で考えるべきは、実装より公開する型だった
codeegg
0
240
エンジニアにデザインハーネスを 〜デザインプロセスを規定するためのハーネス〜 / Design harness from an engineer's perspective
rkaga
2
1.5k
フィードバックで育てるAI開発
kotaminato
1
120
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
0
240
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
340
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.8k
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
10k
Paper Plane (Part 1)
katiecoart
PRO
1
9.7k
Product Roadmaps are Hard
iamctodd
55
12k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.4k
Claude Code のすすめ
schroneko
67
230k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
360
30 Presentation Tips
portentint
PRO
1
350
So, you think you're a good person
axbom
PRO
2
2.1k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
260
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.5k
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ͱ͍͏Ṗͷղܾࡦ ͦΜͳʹίʔυ͘ͳΒͳͯ͘൵͍͠