Created
October 28, 2019 19:25
-
-
Save rustle/6a096a861ea97446d81d28fa008bd48e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@propertyWrapper | |
struct ErasedToAnyPublisher<Upstream: Publisher> { | |
var wrappedValue: AnyPublisher<Upstream.Output, Upstream.Failure> { | |
upstream.eraseToAnyPublisher() | |
} | |
let upstream: Upstream | |
init(upstream: Upstream) { | |
self.upstream = upstream | |
} | |
} | |
struct Foo { | |
enum Error: Swift.Error { | |
case bar | |
} | |
... | |
@ErasedToAnyPublisher<String, Error> var publisher: AnyPublisher<String, Error> | |
init(a: A) { | |
_publisher = ErasedToAnyPublisher(upstream: PassthrougSubject<String, Error>()) | |
... | |
} | |
init(b: B) { | |
_publisher = ErasedToAnyPublisher(upstream: PassthrougSubject<String, Error>()) | |
... | |
} | |
func baz() { | |
_publisher.upstream.send("") | |
} | |
... | |
} | |
let foo = Foo(...) | |
foo.publisher.sink { print($0) } // publisher: AnyPublisher<String, Error> | |
// I'd be much happier if it was closer to | |
struct Foo { | |
enum Error: Swift.Error { | |
case bar | |
} | |
@ErasedToAnyPublisher var publisher = ErasedToAnyPublisher(upstream: PassthrougSubject<String, Error>()) | |
... | |
init(a: A) { | |
... | |
} | |
init(b: B) { | |
... | |
} | |
func baz() { | |
_publisher.upstream.send("") | |
} | |
... | |
} | |
let foo = Foo(...) | |
foo.publisher.sink { print($0) } // publisher: AnyPublisher<String, Error> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment