8

I can see that Subject.eraseToAnySubject() returns the concrete Subject type AnySubject. I'm assuming this is using a type eraser pattern.

However, the apple docs provide almost no details: https://developer.apple.com/documentation/combine/passthroughsubject/3241547-erasetoanysubject

Can anyone explain how this works and where it should be used?

Also, would it be possible to use the some keyword to avoid using AnySubject?

Andrew Paul Simmons
  • 4,334
  • 3
  • 31
  • 39

1 Answers1

17

In Combine, as you chain Publishers to Operators, the return type becomes complicated very quickly since it includes specific detail about each publisher in the chain.

For example a simple string Publisher with a filter and map Operator attached will have a return type of: <Filter<Map<Published<String, Error>>>>

eraseToAny uses a type eraser pattern to capture what's actually important about the return type. In the example given, adding an eraseToAnyPublisher will shorten the type to a more succinct <AnyPublisher<String, Error>>

emma ray
  • 13,336
  • 1
  • 24
  • 50