We have many type erasure classes like AnyPublisher
in Combine
framework. But why do need them at all, as we already have parameterized protocols?
For example, why don't we just replace let myPublisher = AnyPublisher<Int, Never>
with any Publisher<Int, Never>
? The only difference I can tell is that type erasure gives us existential types, but why do we care?
But why do need them at all, as we already have parameterized protocols? (emphasis added)
Combine (released in iOS 13) is several years older than primary associated types (what you're calling "parameterized protocols"). While most things you can do with AnyPublisher you can today do with any Publisher
, that was not true a pretty short time ago. Combine has had almost no updates in several years since async
came out (and most of the updates were to provide async
compatibility). If you were designing Combine from scratch today, you probably could get away without AnyPublisher. But that wasn't true when it was designed, and it is all but abandoned by Apple, so I wouldn't expect major changes.