13

What is the role of AnyPublisher in Combine, and why in many examples, including inWWDC Combine In practice, 27:40 they return AnyPublisher, using .eraseToAnyPublisher, and not just return a Publisher?

The Apple Documents says

Use AnyPublisher to wrap a publisher whose type has details you don’t want to expose to subscribers or other publishers.

But can anyone give an example of where it can be useful?

1
  • 8
    You should read up on type erasure. This article nicely explains the reasoning behind type erasure and why you would want to use it. Bear in mind that Swift 5.1's opaque return types solves the issue of not being able to use PATs as concrete types, which was previously one of the main reasons for using type erasure, but the other use cases still apply. Aug 28, 2019 at 10:39

2 Answers 2

11

Publisher is a Protocol with associated types, while AnyPublisher is a struct.

Try casting to Publisher and you get an error

let x = Just(1) as Publisher

Protocol 'Publisher' can only be used as a generic constraint because it has Self or associated type requirements

This despite the fact that Just is a Publisher.

The Publisher type can't be utilized in the same way as AnyPublisher to achieve type erasure.

Where you could use Publisher is when you define a function that has generics as part of the definition.

Most common reason to use AnyPublisher:

Return an instance of a Publisher from a function.

Most common reason to use Publisher:

Create a protocol extension to create a custom Combine operator. For example:

extension Publisher {
  public func compactMapEach<T, U>(_ transform: @escaping (T) -> U?)
    -> Publishers.Map<Self, [U]>
    where Output == [T]
  {
    return map { $0.compactMap(transform) }
  }
}
3
  • What about just using some Publisher? That works
    – smat88dd
    Jul 14, 2020 at 5:08
  • @smat88dd if you have a better answer, please add it!
    – Gil Birman
    Jul 14, 2020 at 21:42
  • @smat88dd I thinks some Publisher will cause some error when compiler converting to concrete type. So it's not an option. Sep 14, 2020 at 13:09
3

Publisher is a protocol and AnyPublisher is a concrete implementation of Publisher.

AnyPublisher is a type-erased struct that conforms the Publisher protocol. Type erasure allows to hide details about the publisher that may not want to expose to subscribers — or downstream publishers.

Note: AnyPublisher does not have a send(_:) operator, so new values cannot be added to that publisher.

2
  • 1
    this seems to come from RayWenderlich.com. Provide credit if true.
    – fingia
    Aug 7, 2022 at 0:31
  • Note: Publisher does not have a send(_:) operator. That is defined on the Subject protocol, which conforms to and extends Publisher.
    – RTHarston
    Nov 3, 2022 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.