From 46f9bf0c1856222be05805331708b1922a72f978 Mon Sep 17 00:00:00 2001 From: Jules Ivanic Date: Wed, 13 Nov 2024 18:03:35 +1100 Subject: [PATCH] Add `IdentityBoth` instance for `zio.prelude.data.Optional` --- .../scala/zio/prelude/AssociativeBoth.scala | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala b/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala index d4d7e46a4..6cba364cd 100644 --- a/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala +++ b/core/shared/src/main/scala/zio/prelude/AssociativeBoth.scala @@ -18,6 +18,7 @@ package zio.prelude import zio._ import zio.prelude.coherent.CovariantIdentityBoth +import zio.prelude.data.Optional import zio.prelude.newtypes.{AndF, Failure, OrF} import zio.stm.ZSTM import zio.stream.{ZSink, ZStream} @@ -1191,6 +1192,24 @@ object AssociativeBoth extends AssociativeBothLowPriority { } } + /** + * The [[IdentityBoth]] (with [[AssociativeBoth]]) instance for [[Optional]]. + */ + implicit val OptionalnIdentityBoth: IdentityBoth[Optional] = + new IdentityBoth[Optional] { + val any: Optional[Any] = Optional.Present(()) + + def both[A, B](fa: => Optional[A], fb: => Optional[B]): Optional[(A, B)] = + fa match { + case Optional.Present(a) => + fb match { + case Optional.Present(b) => Optional.Present((a, b)) + case Optional.Absent => Optional.Absent + } + case Optional.Absent => Optional.Absent + } + } + /** * The `AssociativeBoth` instance for And `Schedule`. */