Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Empty from EmptyK #4714

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions alleycats-core/src/main/scala/alleycats/Empty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ object Empty extends EmptyInstances0 {
def apply[A](a: => A): Empty[A] =
new Empty[A] { lazy val empty: A = a }

def fromEmptyK[F[_], T](implicit ekf: EmptyK[F]): Empty[F[T]] = ekf.synthesize[T]

/**
* Summon an instance of [[Empty]] for `A`.
*/
Expand Down Expand Up @@ -81,8 +79,12 @@ object Empty extends EmptyInstances0 {

private[alleycats] trait EmptyInstances0 extends compat.IterableEmptyInstance with EmptyInstances1

private[alleycats] trait EmptyInstances1 {
private[alleycats] trait EmptyInstances1 extends EmptyInstances2 {
// If Monoid extended Empty then this could be an exported subclass instance provided by Monoid
implicit def monoidIsEmpty[A: Monoid]: Empty[A] =
Empty(Monoid[A].empty)
}

private[alleycats] trait EmptyInstances2 {
implicit def fromEmptyK[F[_], T](implicit ekf: EmptyK[F]): Empty[F[T]] = ekf.synthesize[T]
}
12 changes: 12 additions & 0 deletions alleycats-core/src/test/scala/alleycats/SyntaxSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ object SyntaxSuite {
val a1: Boolean = x.nonEmpty
}

def testOptionEmpty: Unit = {
case class A[T](x: T)
case class B()

import alleycats.std.option.*
implicit def emptyA[T: Empty]: Empty[A[T]] = new Empty[A[T]] {
def empty: A[T] = A(Empty[T].empty)
}

Empty[A[Option[B]]].empty
}

def testFoldable[F[_]: Foldable, A]: Unit = {
val x = mock[F[A]]
val y = mock[A => Unit]
Expand Down