{ 完整節目筆記 https://weakself.dev/episodes/13 }
又是歡樂的一集!今天聊起 #weakself挑戰賽的 Type Erasure 與 Opaque Return Type。不過,怎麼某人的聲音到後半段就 erased 了!?
前言
上一集成為最熱門集數,所以跟新朋友打招呼、解釋一下我們的名稱
weak self 這個名稱,寫 Swift 或 iOS 工程師一看就知道。但我們其實沒有中文名稱,大家可以幫忙想
weak self, strong community
鐵人賽 iOS Developer Learning Android 系列 by @MarkFlyyyyy 30 天了解 Swift 的 Combine 最高觀看數 bug? 其他整理請見«13的開發者週報»
我恨 PAT
PAT 是什麼?看 code
PAT 三不能:
不能做為變數型別用
不能做為 function 的 return
不能放在 Collection 裡
你是忘記還是害怕想起:
Type Erasure 為何而生
PAT 難搞的地方有兩種:
有
Self
的 Protocol:Hashable
有
associatedtype
的 Protocol:IteratorProtocol
對應的做法:
Opaque Return Type
weakself 的中文名稱是個 Opaque Type (Caller 決定 generic 是什麼)
Opaque Return Type 則由 Callee 決定 generic 是什麼
避免 generiception:
public typealias LazyCompactMapCollection<Elements, ElementOfResult>
-> <C: Collection> C where C.Element == ElementOfResult
= LazyMapSequence<LazyFilterSequence<LazyMapSequence<Elements, ElementOfResult?>>, ElementOfResult>
也是對於 Type Erasure 的一種根本性解決手法
範例請見:https://gist.github.com/pofat/a411d785e593982868a54e140f48ad8f
那第三個問題,不能把 PAT 放入 collection 呢?
抱歉,還無法🤷♂️
更多 weak self
Twitter @weak_self
Share this post