Skip to content

Commit

Permalink
Improve orElse, to auto widen for sum types and remove ClassTags (zio…
Browse files Browse the repository at this point in the history
  • Loading branch information
987Nabil authored Mar 28, 2024
1 parent b4f8533 commit 28930b3
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions zio-schema/shared/src/main/scala-3/zio/schema/package.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 28930b3

Please sign in to comment.