Skip to content

Commit

Permalink
Replace OrElse with delegated instance
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed Jun 1, 2021
1 parent 8a49e4e commit 5d37c1f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
22 changes: 12 additions & 10 deletions core/src/main/scala-3/cats/derived/empty.scala
Expand Up @@ -6,23 +6,25 @@ import shapeless3.deriving.K0
object empty extends EmptyDerivation

trait DerivedEmpty[A] extends Empty[A]:
protected def value(): A
lazy val empty: A = value()
protected def emptyValue(): A
lazy val empty: A = emptyValue()

object DerivedEmpty:
type EmptyOrDerived[A] = Empty[A] OrElse DerivedEmpty[A]
object DerivedEmpty extends DerivedEmptyLowPriority:
given delegated[A](using A: => Empty[A]): DerivedEmpty[A] =
() => A.empty

def product[A](using inst: K0.ProductInstances[EmptyOrDerived, A]): DerivedEmpty[A] =
() => inst.construct([A] => (F: EmptyOrDerived[A]) => F.unify.empty)
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[EmptyOrDerived, gen.MirroredElemTypes, A].unify.empty
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] => product
case given K0.CoproductGeneric[A] => coproduct
case given K0.ProductGeneric[A] => DerivedEmpty.product
case given K0.CoproductGeneric[A] => DerivedEmpty.coproduct

trait EmptyDerivation:
extension (E: Empty.type)
inline def derived[A](using instance: DerivedEmpty[A]): Empty[A] = instance
def derived[A](using instance: DerivedEmpty[A]): Empty[A] = instance
21 changes: 0 additions & 21 deletions core/src/main/scala-3/cats/derived/orelse.scala

This file was deleted.

2 changes: 1 addition & 1 deletion core/src/test/scala-3/cats/derived/EmptyTests.scala
Expand Up @@ -5,7 +5,7 @@ import cats._
import cats.derived.all._

class EmptyTests:
case class Foo(i: Int, b: Option[IntTree]) derives Empty
case class Foo(i: Int, b: IntTree) derives Empty
enum IntTree:
case Leaf
case Node(left: IntTree, value: Int, right: IntTree)

0 comments on commit 5d37c1f

Please sign in to comment.