Skip to content

Commit

Permalink
Added demo back cover copy #bruce #time 45m
Browse files Browse the repository at this point in the history
Also eliminated Scope.default from example
  • Loading branch information
Bruce Eckel committed Jul 18, 2024
1 parent b215b0b commit 774721d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion BookCover.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ Subtitle: A New Paradigm for Creating Reliable, Adaptable, Testable Systems
Authors: Bill Frasure, Bruce Eckel, and James Ward
ISBN for print book: 978-0-9818725-7-5
Cover Price: $35
Back Cover Copy: TODO
Back Cover Copy:
A new paradigm called "Effect Oriented Programming" has emerged for building more reliable, adaptable, and testable systems.
By separating "side effects'' from the "pure" parts of a program you gain some super-powers for handling errors, applying timeouts & retries, and testing unreliable parts in isolation.
This book introduces the concepts of Effect Oriented Programming using simplified code examples and clear explanatory prose.
4 changes: 2 additions & 2 deletions Chapters/03_Superpowers.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ The retries do not succeed so the fallback is applied.

## Finalization

To ensure that something happens after an Effect completes, regardless of failures, we use `withFinalizer`:
To ensure that something happens after an Effect completes, regardless of failures, we can attach `withFinalizer` to any Effect:

```scala 3 mdoc:silent
val effect5 =
Expand All @@ -331,7 +331,7 @@ val effect5 =
```

`withFinalizer` expects a function as its argument; `_ => logUserSignup` creates a function that takes no arguments and calls `logUserSignup`.
`withFinalizer` attaches this behavior without changing the types contained in the original Effect.
`withFinalizer` attaches this behavior with no impact on the types contained in the original Effect.

```scala 3 mdoc:runzio
override val bootstrap = happyPath
Expand Down
15 changes: 11 additions & 4 deletions Chapters/04_Initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,15 @@ To provide `BreadHomeMade`, we need `Dough` and an `Oven`.

## Dependency Cleanup

We briefly saw `withFinalizer` in the [Superpowers](03_Superpowers.md) chapter.
There's more to it, however.
How does `withFinalizer` know when to perform its finalization?
It uses `ZIO`'s *scoping*.

[[ Maybe an intro example here; we'll see ]]

### A Safer Oven

An Effect without outstanding dependencies can be used to construct a `ZLayer`.
This will correct a dangerous oversight: we heat up our `Oven`, but never turn it off!
An `OvenSafe` turns itself off when it is no longer needed.
Expand All @@ -526,14 +535,13 @@ import zio.Console.*

object OvenSafe:
val heated =
ZLayer.fromZIO:
ZLayer.scoped:
defer:
printLine("Oven: Heated").run
Oven()
.withFinalizer:
_ =>
printLine("Oven: Turning off")
.orDie
printLine("Oven: Turning off").orDie
```

```scala 3 mdoc:runzio
Expand All @@ -545,7 +553,6 @@ def run =
BreadHomeMade.baked,
Dough.fresh,
OvenSafe.heated,
Scope.default,
)
```

Expand Down

0 comments on commit 774721d

Please sign in to comment.