-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Port EmptyK to dotty * Fix formatting * Support EmptyK for sum types * Refactor EmptyKSuite Co-authored-by: Georgi Krastev <joro.kr.21@gmail.com>
- Loading branch information
1 parent
735d569
commit 9c9123e
Showing
7 changed files
with
145 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package cats.derived | ||
|
||
import alleycats.{Empty, EmptyK, Pure} | ||
import cats.derived.util.Kinds | ||
import shapeless3.deriving.{Const, K1} | ||
|
||
import scala.annotation.implicitNotFound | ||
import scala.compiletime.summonInline | ||
import scala.util.NotGiven | ||
|
||
@implicitNotFound("""Could not derive an instance of EmptyK[F] where F = ${F}. | ||
Make sure that F[_] satisfies one of the following conditions: | ||
* it is a constant type λ[x => T] where T: Empty | ||
* it is a nested type λ[x => G[H[x]]] where G: EmptyK | ||
* it is a nested type λ[x => G[H[x]]] where G: Pure and H: EmptyK | ||
* it is a generic case class where all fields have an EmptyK instance | ||
* it is a generic sealed trait where exactly one subtype has an EmptyK instance | ||
Note: using kind-projector notation - https://github.com/typelevel/kind-projector""") | ||
type DerivedEmptyK[F[_]] = Derived[EmptyK[F]] | ||
object DerivedEmptyK: | ||
type Or[F[_]] = Derived.Or[EmptyK[F]] | ||
inline def apply[F[_]]: EmptyK[F] = | ||
import DerivedEmptyK.given | ||
summonInline[DerivedEmptyK[F]].instance | ||
|
||
given [T](using T: Empty[T]): DerivedEmptyK[Const[T]] = | ||
new EmptyK[Const[T]]: | ||
def empty[A] = T.empty | ||
|
||
given [F[_], G[_]](using F: Or[F]): DerivedEmptyK[[x] =>> F[G[x]]] = | ||
new EmptyK[[x] =>> F[G[x]]]: | ||
def empty[A] = F.unify.empty | ||
|
||
given [F[_], G[_]](using NotGiven[Or[F]])(using F: Pure[F], G: Or[G]): DerivedEmptyK[[x] =>> F[G[x]]] = | ||
new EmptyK[[x] =>> F[G[x]]]: | ||
def empty[A] = F.pure(G.unify.empty) | ||
|
||
given product[F[_]](using inst: K1.ProductInstances[Or, F]): DerivedEmptyK[F] = | ||
new EmptyK[F]: | ||
def empty[A]: F[A] = inst.unify.construct([f[_]] => (E: EmptyK[f]) => E.empty[A]) | ||
|
||
inline given coproduct[F[_]](using gen: K1.CoproductGeneric[F]): DerivedEmptyK[F] = | ||
Kinds.summonOne1[Or, gen.MirroredElemTypes, F].unify |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright (c) 2015 Miles Sabin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.derived | ||
|
||
import alleycats.{EmptyK, Pure} | ||
import alleycats.std.all.* | ||
import cats.data.NonEmptyList | ||
import cats.laws.discipline.SerializableTests | ||
import cats.syntax.all.* | ||
import shapeless3.test.illTyped | ||
|
||
import scala.compiletime.summonInline | ||
|
||
class EmptyKSuite extends KittensSuite: | ||
import EmptyKSuite.* | ||
import TestDefns.* | ||
|
||
given Pure[Box] with | ||
def pure[A](a: A) = Box(a) | ||
|
||
inline def emptyK[F[_]] = | ||
summonInline[EmptyK[F]].empty | ||
|
||
inline def testEmptyK(context: String): Unit = | ||
test(s"$context.EmptyK[LOption]")(assert(emptyK[LOption] == Nil)) | ||
test(s"$context.EmptyK[PList]")(assert(emptyK[PList] == (Nil, Nil))) | ||
test(s"$context.EmptyK[CaseClassWOption]")(assert(emptyK[CaseClassWOption] == CaseClassWOption(None))) | ||
test(s"$context.EmptyK[NelOption]")(assert(emptyK[NelOption] == NonEmptyList.one(None))) | ||
test(s"$context.EmptyK[IList]")(assert(emptyK[IList] == INil())) | ||
test(s"$context.EmptyK[Snoc]")(assert(emptyK[Snoc] == SNil())) | ||
test(s"$context.EmptyK respects existing instances")(assert(emptyK[BoxColor] == Box(Color(255, 255, 255)))) | ||
checkAll(s"$context.EmptyK is Serializable", SerializableTests.serializable(summonInline[EmptyK[LOption]])) | ||
|
||
locally { | ||
import auto.emptyK.given | ||
testEmptyK("auto") | ||
} | ||
|
||
locally { | ||
import semiInstances.given | ||
testEmptyK("semiauto") | ||
} | ||
|
||
end EmptyKSuite | ||
|
||
object EmptyKSuite: | ||
import TestDefns.* | ||
|
||
type LOption[A] = List[Option[A]] | ||
type PList[A] = (List[A], List[A]) | ||
type NelOption[A] = NonEmptyList[Option[A]] | ||
type BoxColor[A] = Box[Color[A]] | ||
|
||
object semiInstances: | ||
given EmptyK[LOption] = semiauto.emptyK | ||
given EmptyK[PList] = semiauto.emptyK | ||
given EmptyK[CaseClassWOption] = semiauto.emptyK | ||
given EmptyK[NelOption] = semiauto.emptyK | ||
given EmptyK[IList] = semiauto.emptyK | ||
given EmptyK[Snoc] = semiauto.emptyK | ||
given EmptyK[BoxColor] = semiauto.emptyK | ||
|
||
final case class Color[A](r: Int, g: Int, b: Int) | ||
object Color: | ||
given EmptyK[Color] with | ||
def empty[A] = Color(255, 255, 255) | ||
|
||
end EmptyKSuite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters