diff --git a/core/src/main/scala-3/cats/derived/empty.scala b/core/src/main/scala-3/cats/derived/empty.scala index ff192a8c..76b5c849 100644 --- a/core/src/main/scala-3/cats/derived/empty.scala +++ b/core/src/main/scala-3/cats/derived/empty.scala @@ -2,6 +2,7 @@ package cats.derived import alleycats.Empty import shapeless3.deriving.K0 +import scala.compiletime.summonFrom object empty extends EmptyDerivation @@ -9,22 +10,26 @@ trait DerivedEmpty[A] extends Empty[A]: protected def emptyValue(): A lazy val empty: A = emptyValue() -object DerivedEmpty extends DerivedEmptyLowPriority: - given delegated[A](using A: => Empty[A]): DerivedEmpty[A] = +object DerivedEmpty: + inline given [A]: DerivedEmpty[A] = summonFrom { + case given Empty[A] => delegated + case given K0.Generic[A] => derived + } + + def delegated[A](using A: => Empty[A]): DerivedEmpty[A] = () => A.empty + inline def derived[A](using gen: K0.Generic[A]): DerivedEmpty[A] = + inline gen match + case given K0.ProductGeneric[A] => DerivedEmpty.product + case given K0.CoproductGeneric[A] => DerivedEmpty.coproduct + def product[A](using inst: K0.ProductInstances[DerivedEmpty, A]): DerivedEmpty[A] = () => inst.construct([A] => (A: DerivedEmpty[A]) => A.empty) inline def coproduct[A](using gen: K0.CoproductGeneric[A]): DerivedEmpty[A] = K0.summonFirst[DerivedEmpty, gen.MirroredElemTypes, A] -private[derived] sealed abstract class DerivedEmptyLowPriority: - inline given derived[A](using gen: K0.Generic[A]): DerivedEmpty[A] = - inline gen match - case given K0.ProductGeneric[A] => DerivedEmpty.product - case given K0.CoproductGeneric[A] => DerivedEmpty.coproduct - trait EmptyDerivation: extension (E: Empty.type) def derived[A](using instance: DerivedEmpty[A]): Empty[A] = instance