Last active
January 2, 2020 05:39
-
-
Save layoutSubviews/4f06aac1ba2bf26a1da9f020103afb55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Combine | |
func stringIfOver42(_ value: Int) -> String? { | |
value > 42 ? "\(value)" : nil | |
} | |
let voidSubject = PassthroughSubject<Void, Never>() | |
let intSubject = PassthroughSubject<Int, Never>() | |
let publisher = voidSubject | |
// Changing "String?" to "String" below fixes the bug. | |
.map { () -> AnyPublisher<String?, Never> in | |
intSubject | |
.compactMap { stringIfOver42($0) } | |
.eraseToAnyPublisher() | |
} | |
.switchToLatest() | |
let cancellable = publisher.sink { print($0) } | |
voidSubject.send(()) | |
// This should not print anything, but does | |
intSubject.send(10) | |
// This should print "50" | |
intSubject.send(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment