Skip to content

Instantly share code, notes, and snippets.

@mbrandonw
Last active June 24, 2022 15:55
Show Gist options
  • Save mbrandonw/cce1b6113703cce6a260e027ad38b1a6 to your computer and use it in GitHub Desktop.
Save mbrandonw/cce1b6113703cce6a260e027ad38b1a6 to your computer and use it in GitHub Desktop.

Add primary associated types to Publisher protocol

FB10233455

Sometimes it's necessary to construct a large, complex publisher and return it from a function. Currently you either need to explicitly return that type from the function, which is difficult and breaks compilation with any change to the publisher:

func onAppearEffects() -> Publishers.Zip<...> {
  fetchUser()
    .zip(with: fetchRespositories())
    .zip(with: fetchFavorites())
}

Or you have to erase the publisher:

func onAppearEffects() -> AnyPublisher<Action, Never> {
  fetchUser()
    .zip(with: fetchRespositories())
    .zip(with: fetchFavorites())
    .eraseToAnyPublisher()
}

If Publisher had primary associated types then you could use some Publisher:

func onAppearEffects() -> some Publisher<Action, Never> {
  fetchUser()
    .zip(with: fetchRespositories())
    .zip(with: fetchFavorites())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment