How does Effect differ from Publisher?

I'm taking some time to learn about the Swift Composable Architecture (as well as Combine) to see what ideas we can borrow for our own ad hoc architecture that shares many similarities (but uses RxSwift).

As I look at the sources for Effect, it seems to be a small wrapper around Publisher, but it is not immediately obvious to me why it needs to exist at all. A lot of the convenience methods seem like they could have just been declared in an extension on Publisher. I imagine my inexperience with Combine is what is causing my confusion, but I'd figure I'd ask.

1 Like

Speculating, I see three reasons:

  • They can add members to Effect without risking collisions with current or future Publisher APIs.

  • They can add initializers to Effect. For example, you can say Effect(value: 123). Since Publisher is a protocol, you cannot call an initializer directly on it. You cannot define an init that lets you say Publisher(value: 123). Instead you have to use a conforming type like Just(123).

  • They can add static members to Effect and you can refer to them with dot-syntax. For example, you can say .none or .future(...) or .result(...) in a context where the type is deduced as Effect. You cannot use dot-syntax to create a Publisher, even in a context where the type is deduced as conforms-to-Publisher.

1 Like

@hwkrvn thanks for the question, and I think @mayoff summed up the reasons quite well. It mostly started as the first (not wanting to pollute Publisher and AnyPublisher with a bunch of TCA-specific things), but there are other benefits too.

1 Like

@mayoff and @mbrandonw thank you both! The given reasons seem practical (and a bit obvious in hindsight :sweat_smile:), which is exactly what I was hoping for.

Terms of Service

Privacy Policy

Cookie Policy