Kevin ABRIOUX
Kevin ABRIOUX

Reputation: 17735

Cast AnyPublisher to Publisher in Combine

I am using a Publisher<Int,Never> in combine, and I am trying to return a Published<Int>.Publisher

It is possible in combine to transform an AnyPublisher<Int,Never> into a Published<Int>.Publisher ?

Upvotes: 1

Views: 1088

Answers (1)

David Pasztor
David Pasztor

Reputation: 54775

No, this is not possible. AnyPublisher is a type erased version of Publisher. Once you type erase a variable, the original type information is lost (or at the very least hidden) for the lifetime of the object, you cannot convert it back to the original type.

If you need to return a Published<Int>.Publisher, simply make that the return type of your function. Published<Int>.Publisher is a struct, so you can use it as a return type (unlike Publisher, which is a protocol with associated types).

Upvotes: 7

Related Questions