You can then centralize the error handling by having a "stderr"-like output on your filters.
When you have a call/return architectural style, you need to return something, and thread the result along, making things more complicated.
That is an important root insight. It also applies to for loops, and could apply to switch statements
switch result { case something: break case none: error: empty: default: //? }
Values are much easier to reason about and inspect than functions, so it's better to have simple functions that return complicated values than to complicate the functions themselves.
And you know this how?
> Values are much easier to reason about and inspect than functions
Right, which is why we want to avoid functions as much as possible.
> simple functions that return complicated values than to complicate the functions themselves
Fortunately, these filters aren't functions, so this doesn't complicate them, it makes them simpler.
Professional experience.
> Fortunately, these filters aren't functions, so this doesn't complicate them, it makes them simpler.
Functions are at least restricted enough that you can sort of reason about them. If these things are not even functions then there's no hope of ever understanding them.
- (void)fetchData whenDone finished: [a block] ifFails failed: [another block]
Or: - (void)fetchData whenDone finished: [a block that has nullable Data and nullable error as parameters]
Both approaches had problems. The first one uses two result blocks, but you want to do a lot of the same stuff usually, like:* Stop the spinner
* Enable the send button again
This often lead to repetitive code or a maze of function calls.
The second one made you basically duck type for the kind of response it really was. Does it have an error? It probably failed. Does it have data? It probably worked. But what if it has both data and an error (error data?) or simply both values were nil? Brittle code.
While I really like the railroad approach the typical problem area Swift is applied to (often web API driven iOS and macOS applications) simply don't lead to these huge chains of functions. I'm definitely going to try it for backend code though.
I agree you don't see them collected together, as a rule, but if you follow Results<> like this in those domains you see them being used multiple times on the backend before being yeilded to the frontend for further handling, resulting in chains that are just spread out a little bit more.
Personally I've started exposing them through JSON at the API level to further extend the systems explicitness and idempotency.
Also, compare it to the interface of Futures.
Using exceptions is dealing with errors out-of-band. Using Result types is dealing with errors in-band. In-band is somewhat less flexible because you don't have the power to arbitrarily alter the execution flow of your program. Out-of-band is much easier to reason about by that very token.