Skip to content

Instantly share code, notes, and snippets.

@joshuajhomann
Last active August 12, 2022 14:36
Show Gist options
  • Save joshuajhomann/60fcee09bf623f1033cb5be22f4c7a62 to your computer and use it in GitHub Desktop.
Save joshuajhomann/60fcee09bf623f1033cb5be22f4c7a62 to your computer and use it in GitHub Desktop.
import Combine
//SE-361 Extensions on bound generic types (also 244 & 346)
extension Publisher<Int, Never> {
func make(byAppending value: Output) -> some Publisher<Output, Failure> {
append(value)
}
}
let p = [0].publisher.make(byAppending: 1)
//SE-244 Opaque Result Types
func makeIntPublisher<Value>(value: Value) -> some Publisher<Value, Never> {
CurrentValueSubject<Value, Never>(value)
}
//SE-346 Lightweight same-type requirements for primary associated types
//SE-335 Existential any
let publishers: [any Publisher<Int, Never>] = [
p,
Just(2),
makeIntPublisher(value: 3)
]
//SE-328 Structural opaque result types
//SE-352 Implicitly Opened Existentials
func printValues(for publisher: some Publisher<some CustomStringConvertible, Never>) -> AnyCancellable {
publisher.sink { print($0) }
}
var subscriptions: Set<AnyCancellable> = []
for publisher in publishers {
printValues(for: publisher).store(in: &subscriptions)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment