NOTE: please don't miss EDIT 2, the idea is there! thx
Today's syntax:
publisher.sink(receiveCompletion: { completion in
// handle error
}) { data in
// handle data
}
Tomorrow's syntax:
publisher.sink { data in
// handle data
} receiveCompletion: { completion in
// handle error
}
Requested controversial syntax:
publisher.sink receiveValue: { data in
// handle data
} receiveCompletion: { completion in
// handle error
}
Proposed, more Swifty syntax:
publisher.sink receive(value data): { // data parameter name is here optional
// handle data
} receive(completion): {
// handle error
}
with a new declaration for sink:
func sink(
receive receiveCompletion: @escaping ((completion: Subscribers.Completion<Self.Failure>) -> Void),
receive receiveValue: @escaping ((value: Self.Output) -> Void)
) -> AnyCancellable
What do you think? Crazy idea?
EDIT: (you can skip it, go to EDIT 2)
func sink(
receive: @escaping ((_ completion: Subscribers.Completion<Self.Failure>) -> Void),
receive: @escaping ((value: Self.Output) -> Void)
) -> AnyCancellable
with mandatory value:
label (but optional '_ completion:'
) (to disambiguate receive:
label used twice) should be more correct in this example... I guess.
EDIT 2: -----------------------------------------------------------------
The cleanest declaration should be:
func sink(
received: @escaping ((value: Self.Output) -> Void),
received: @escaping ((completion: Subscribers.Completion<Self.Failure>) -> Void)
) -> AnyCancellable
and simplest usage:
publisher.sink received(value): { // `received` label is optional => publisher.sink (value): {
print(value)
} received(completion): {
handleError(from: completion)
}
but also (if desired):
publisher.sink received(value data): { // `value` acting as label name, `data` as user chosen parameter name
print(data)
} received(completion _): { // parameter not needed
fatalError()
}