We're sunsetting PodQuest on 2025-07-28. Thank you for your support!
Export Podcast Subscriptions
cover of episode 13: 喬喬 Erasure

13: 喬喬 Erasure

2019/10/21
logo of podcast weak self

weak self

Shownotes Transcript

又是歡樂的一集!今天聊起 #weakself挑戰賽)的 Type Erasure 與 Opaque Return Type。不過,怎麼某人的聲音到後半段就 erased 了!? { 完整節目筆記 https://weakself.dev/episodes/13) }

前言

我恨 PAT

PAT 是什麼?看 code) PAT 三不能:

  • 不能做為變數型別用

  • 不能做為 function 的 return

  • 不能放在 Collection 裡

你是忘記還是害怕想起:

Type Erasure 為何而生

PAT 難搞的地方有兩種:

  • Self 的 Protocol:Hashable

  • associatedtype 的 Protocol:IteratorProtocol

對應的做法:

  • AnyHashable: 黑魔法 C++

  • AnyIterator: 用 generic struct 解決 範例請見這裡)

  • 對 AnyHashable 有興趣請右轉)

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>`

那第三個問題,不能把 PAT 放入 collection 呢?

抱歉,還無法🤷‍♂️

更多 weak self

-- Hosting provided by SoundOn)