Skip to content

Commit

Permalink
'case' removed when possible from Initialization #bruce #time 10m
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce Eckel committed Jul 22, 2024
1 parent 38eec60 commit 56fa76b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 8 additions & 12 deletions Chapters/04_Initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ The simplest approach is to buy `Bread` from the store:
import zio.*
import zio.direct.*

case class BreadStoreBought() extends Bread
class BreadStoreBought extends Bread

object BreadStoreBought:
val purchased =
ZLayer.succeed:
BreadStoreBought()
```

In this book, we follow the Scala practice of preferring `case` classes over ordinary classes.{i: "case class"}{i: "class, case"}
`case` classes are immutable by default and automatically provide commonly-needed functionality.
You aren't required to use `case` classes to work with the Effect system, but they provide valuable conveniences.

The companion object `BreadStoreBought` contains a single value called `purchased`.
This produces a special kind of Effect: the `ZLayer`.{i: "ZLayer"}
The Effect System uses `ZLayer`s to automatically inject dependencies.
Expand Down Expand Up @@ -148,7 +144,7 @@ import zio.*
import zio.Console.*
import zio.direct.*

case class X():
class X:
val display = printLine("X.display")

val makeX =
Expand Down Expand Up @@ -201,7 +197,7 @@ The `ZLayer` is the holder for the object produced by `make`, and it provides a
Adding trace information to the previous example reveals the steps of program initialization:{i: "initialization, steps"}

```scala 3 mdoc:silent
case class Y():
class Y:
val display = printLine("Y.display")

val makeY =
Expand Down Expand Up @@ -271,7 +267,7 @@ import zio.*
import zio.direct.*
import zio.Console.*

case class Dough():
class Dough:
val letRise = printLine("Dough: rising")
```

Expand Down Expand Up @@ -311,7 +307,7 @@ import zio.direct.*
import zio.Console.*

trait HeatSource
case class Oven() extends HeatSource
class Oven extends HeatSource

object Oven:
val heated =
Expand All @@ -330,7 +326,7 @@ import zio.*
import zio.direct.*
import zio.Console.*

case class BreadHomeMade(
class BreadHomeMade(
heat: HeatSource,
dough: Dough,
) extends Bread
Expand Down Expand Up @@ -414,7 +410,7 @@ A `Toaster` is a `HeatSource`:
import zio.*
import zio.direct.*

case class Toaster() extends HeatSource
class Toaster extends HeatSource

object Toaster:
val ready =
Expand Down Expand Up @@ -560,7 +556,7 @@ import zio.*
import zio.direct.*
import zio.Console.*

case class BreadFromFriend() extends Bread()
class BreadFromFriend extends Bread()

object Friend:
def forcedFailure(invocations: Int) =
Expand Down
4 changes: 4 additions & 0 deletions graveyard/CaseClassExplanation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
In this book, we follow the Scala practice of preferring `case` classes over ordinary classes.{i: "case class"}{i: "class, case"}
`case` classes are immutable by default and automatically provide commonly needed functionality.
You aren't required to use `case` classes to work with the Effect system, but they provide valuable conveniences.

0 comments on commit 56fa76b

Please sign in to comment.