From 28930b3fe828ed6ca30273d2218df92f9daac4fa Mon Sep 17 00:00:00 2001 From: Nabil Abdel-Hafeez <7283535+987Nabil@users.noreply.github.com> Date: Thu, 28 Mar 2024 14:16:12 +0100 Subject: [PATCH] Improve orElse, to auto widen for sum types and remove ClassTags (#670) --- .../src/main/scala-3/zio/schema/package.scala | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/zio-schema/shared/src/main/scala-3/zio/schema/package.scala b/zio-schema/shared/src/main/scala-3/zio/schema/package.scala index 57f1f2b5b..dbbf2dd7e 100644 --- a/zio-schema/shared/src/main/scala-3/zio/schema/package.scala +++ b/zio-schema/shared/src/main/scala-3/zio/schema/package.scala @@ -1,23 +1,22 @@ package zio -import scala.reflect.ClassTag - package object schema { type Singleton = scala.Singleton - extension [A: ClassTag, B: ClassTag] (s: Schema.Fallback[A, B]) - def merge: Schema[A | B] = - s.transform(_.merge, { - case left: A => Fallback.Left(left.asInstanceOf[A]) - case right: B => Fallback.Right(right.asInstanceOf[B]) - }) + extension [A <: C, B <: C, C](s: Schema.Fallback[A, B]) + transparent inline def merge: Schema[C] = + s.transform( + _.merge, { + case left: A => Fallback.Left(left) + case right: B => Fallback.Right(right) + }) - extension [A: ClassTag] (s: Schema[A]) - def orElse[B: ClassTag](s0: Schema[B]): Schema[A | B] = + extension [A <: C, C](s: Schema[A]) + transparent inline def orElse[B <: C](s0: Schema[B]): Schema[C] = Schema.Fallback(s, s0).merge - extension [A: ClassTag, B: ClassTag] (fallback: Fallback[A, B]) - def merge: A | B = + extension [A <: C, B <: C, C](fallback: Fallback[A, B]) + transparent inline def merge: C = fallback match { case Fallback.Left(left) => left case Fallback.Right(right) => right