Skip to content

Commit

Permalink
Prevent allocations on ZPure.unit and ZPure.none (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostdogpr authored Apr 3, 2024
1 parent c52b32e commit 2b3f6f1
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions core/shared/src/main/scala/zio/prelude/fx/ZPure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ sealed trait ZPure[+W, -S1, +S2, -R, +E, +A] { self =>
final def none[B](implicit ev: A <:< Option[B]): ZPure[W, S1, S2, R, Option[E], Unit] =
self.foldM(
e => ZPure.fail(Some(e)),
a => a.fold[ZPure[W, S2, S2, R, Option[E], Unit]](ZPure.succeed(()))(_ => ZPure.fail(None))
a => a.fold[ZPure[W, S2, S2, R, Option[E], Unit]](ZPure.unit)(_ => ZPure.fail(None))
)

/**
Expand Down Expand Up @@ -972,6 +972,8 @@ sealed trait ZPure[+W, -S1, +S2, -R, +E, +A] { self =>
}

object ZPure {
private val succeedUnit: ZPure[Nothing, Any, Nothing, Any, Nothing, Unit] = Succeed(())
private val succeedNone: ZPure[Nothing, Any, Nothing, Any, Nothing, Option[Nothing]] = Succeed(None)

/**
* Constructs a computation, catching any `Throwable` that is thrown.
Expand Down Expand Up @@ -1164,7 +1166,7 @@ object ZPure {
* Constructs a computation that succeeds with the `None` value.
*/
def none[S]: ZPure[Nothing, S, S, Any, Nothing, Option[Nothing]] =
succeed(None)
succeedNone

/**
* Accesses the specified service in the environment of the computation.
Expand Down Expand Up @@ -1209,7 +1211,7 @@ object ZPure {
* state through unchanged.
*/
def unit[S]: ZPure[Nothing, S, S, Any, Nothing, Unit] =
succeed(())
succeedUnit

/**
* The moral equivalent of `if (!p) exp`
Expand Down

0 comments on commit 2b3f6f1

Please sign in to comment.