Combine seems to be built with type-erasure in mind. One reason is to avoid the complex types that are generated by many chained operators, such as the explanation defined in this question.
I'm curious about the cases where you would not want to call eraseToAnyPublisher()
. I thought of a possible candidate:
func fetchResource() -> Future<Model, Error>
In this case, fetchResource
isn't meant to emit more than once, and giving the return type of Future
would add clarity to the functionality.
You could also return AnyPublisher
:
func fetchResource() -> AnyPublisher<Model, Error>
This allows you to hide the implementation details from the consumer and protect against misuse. There is a tradeoff though... the consumer wouldn't know the semantics of the Future
:
Future
executes as soon as it's created, compared to some publishers that emit values only when there's a subscriptionFuture
retains their eventual result and shares/replays the value to any future subscribers
Anyone know of any good examples of when you wouldn't eraseToAnyPublisher()
?