-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Failure" might be complete #bruce #time 15m
- Loading branch information
Bruce Eckel
committed
Jul 22, 2024
1 parent
445dca9
commit 5a01847
Showing
2 changed files
with
30 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
## Short-circuiting Failures | ||
|
||
Despite the downsides of throwing `Exception`s, there is a reason it is a common practice. | ||
It is a quick and easy way to stop the function when something goes wrong, without wrapping your logic in `if/else`. | ||
|
||
With Effects, we achieve the same behavior without the downsides. | ||
|
||
This triggers a `gpsFailure`, causing the *first* Effect to fail: | ||
|
||
```scala 3 mdoc:runzio | ||
import zio.* | ||
|
||
override val bootstrap = gpsFailure | ||
|
||
def run = | ||
weatherReport | ||
``` | ||
|
||
The program fails with the GPS failure, and the `check` Effect does not run. | ||
|
||
Short-circuiting is an essential part of user-friendly Effect Systems. | ||
With it, we can write a linear sequence of fallible expressions, while tracking all possible failures. |