In the Effect module, similar to other modules like Option and Exit, we have a Effect.match function that allows us to handle different cases simultaneously.
Additionally, Effect provides various functions to manage both success and failure scenarios in effectful programs.
match
The Effect.match function lets you handle both success and failure cases without performing side effects. You provide a handler for each case.
Example (Handling Both Success and Failure Cases)
ignore
If you’re not interested in the success or failure values, you can ignore them:
Example (Ignoring Success and Failure Values)
In this case, we use the constVoid function from the Function module, which constantly returns void, to provide handlers that perform no operation. This effectively discards the success and failure values and focuses solely on the control flow or side effects of the program.
Alternatively, the same result can be achieved using the Effect.ignore function:
Example (Using Effect.ignore to Discard Values)
matchEffect
The Effect.matchEffect function, similar to Effect.match, allows you to handle both success and failure cases, but it also enables you to perform additional side effects within those handlers.
Example (Handling Success and Failure with Side Effects)
matchCause / matchCauseEffect
The Effect.matchCause and Effect.matchCauseEffect functions allow you to handle failures more precisely by providing access to the complete cause of failure within a fiber. This makes it possible to differentiate between various failure types and respond accordingly.
Example (Handling Different Failure Causes with Effect.matchCauseEffect)