-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Derive Band, Semilattice and BoundedSemilattice on Scala 3 (#645)
* Derive Band on Scala 3 * Derive Semilattice on Scala 3 * Derive BoundedSemilattice on Scala 3
- Loading branch information
Showing
8 changed files
with
338 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cats.derived | ||
|
||
import cats.kernel.Band | ||
import shapeless3.deriving.K0.* | ||
|
||
import scala.annotation.* | ||
import scala.compiletime.* | ||
|
||
@implicitNotFound("""Could not derive an instance of Band[A] where A = ${A}. | ||
Make sure that A is a case class where all fields have a Band instance.""") | ||
type DerivedBand[A] = Derived[Band[A]] | ||
object DerivedBand: | ||
type Or[A] = Derived.Or[Band[A]] | ||
|
||
@nowarn("msg=unused import") | ||
inline def apply[A]: Band[A] = | ||
import DerivedBand.given | ||
summonInline[DerivedBand[A]].instance | ||
|
||
@nowarn("msg=unused import") | ||
inline def strict[A]: Band[A] = | ||
import Strict.given | ||
summonInline[DerivedBand[A]].instance | ||
|
||
given product[A](using inst: => ProductInstances[Or, A]): DerivedBand[A] = | ||
Strict.product(using inst.unify) | ||
|
||
trait Product[F[x] <: Band[x], A: ProductInstancesOf[F]] extends DerivedSemigroup.Product[F, A], Band[A] | ||
|
||
object Strict: | ||
given product[A: ProductInstancesOf[Band]]: DerivedBand[A] = | ||
new Product[Band, A] {} |
34 changes: 34 additions & 0 deletions
34
core/src/main/scala-3/cats/derived/DerivedBoundedSemilattice.scala
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,34 @@ | ||
package cats.derived | ||
|
||
import cats.kernel.BoundedSemilattice | ||
import shapeless3.deriving.K0.* | ||
|
||
import scala.annotation.* | ||
import scala.compiletime.* | ||
|
||
@implicitNotFound("""Could not derive an instance of BoundedSemilattice[A] where A = ${A}. | ||
Make sure that A is a case class where all fields have a BoundedSemilattice instance.""") | ||
type DerivedBoundedSemilattice[A] = Derived[BoundedSemilattice[A]] | ||
object DerivedBoundedSemilattice: | ||
type Or[A] = Derived.Or[BoundedSemilattice[A]] | ||
|
||
@nowarn("msg=unused import") | ||
inline def apply[A]: BoundedSemilattice[A] = | ||
import DerivedBoundedSemilattice.given | ||
summonInline[DerivedBoundedSemilattice[A]].instance | ||
|
||
@nowarn("msg=unused import") | ||
inline def strict[A]: BoundedSemilattice[A] = | ||
import Strict.given | ||
summonInline[DerivedBoundedSemilattice[A]].instance | ||
|
||
given product[A](using inst: => ProductInstances[Or, A]): DerivedBoundedSemilattice[A] = | ||
Strict.product(using inst.unify) | ||
|
||
trait Product[F[x] <: BoundedSemilattice[x], A: ProductInstancesOf[F]] | ||
extends DerivedCommutativeMonoid.Product[F, A], | ||
BoundedSemilattice[A] | ||
|
||
object Strict: | ||
given product[A: ProductInstancesOf[BoundedSemilattice]]: DerivedBoundedSemilattice[A] = | ||
new Product[BoundedSemilattice, A] {} |
34 changes: 34 additions & 0 deletions
34
core/src/main/scala-3/cats/derived/DerivedSemilattice.scala
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,34 @@ | ||
package cats.derived | ||
|
||
import cats.kernel.Semilattice | ||
import shapeless3.deriving.K0.* | ||
|
||
import scala.annotation.* | ||
import scala.compiletime.* | ||
|
||
@implicitNotFound("""Could not derive an instance of Semilattice[A] where A = ${A}. | ||
Make sure that A is a case class where all fields have a Semilattice instance.""") | ||
type DerivedSemilattice[A] = Derived[Semilattice[A]] | ||
object DerivedSemilattice: | ||
type Or[A] = Derived.Or[Semilattice[A]] | ||
|
||
@nowarn("msg=unused import") | ||
inline def apply[A]: Semilattice[A] = | ||
import DerivedSemilattice.given | ||
summonInline[DerivedSemilattice[A]].instance | ||
|
||
@nowarn("msg=unused import") | ||
inline def strict[A]: Semilattice[A] = | ||
import Strict.given | ||
summonInline[DerivedSemilattice[A]].instance | ||
|
||
given product[A](using inst: => ProductInstances[Or, A]): DerivedSemilattice[A] = | ||
Strict.product(using inst.unify) | ||
|
||
trait Product[F[x] <: Semilattice[x], A: ProductInstancesOf[F]] | ||
extends DerivedCommutativeSemigroup.Product[F, A], | ||
Semilattice[A] | ||
|
||
object Strict: | ||
given product[A: ProductInstancesOf[Semilattice]]: DerivedSemilattice[A] = | ||
new Product[Semilattice, A] {} |
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,68 @@ | ||
/* | ||
* Copyright (c) 2016 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 cats.kernel.Band | ||
import cats.kernel.laws.discipline.{BandTests, SerializableTests} | ||
|
||
import scala.compiletime.* | ||
|
||
class BandSuite extends KittensSuite: | ||
import ADTs.* | ||
import BandSuite.* | ||
|
||
inline def tests[A]: BandTests[A] = | ||
BandTests[A](using summonInline) | ||
|
||
inline def validate(inline instance: String): Unit = | ||
checkAll(s"$instance[Masked[String]]", tests[Masked[String]].band) | ||
checkAll(s"$instance is Serializable", SerializableTests.serializable(summonInline[Band[Masked[String]]])) | ||
|
||
locally: | ||
import auto.band.given | ||
validate("auto.band") | ||
|
||
locally: | ||
import semiInstances.given | ||
validate("semiauto.band") | ||
|
||
locally: | ||
import strictInstances.given | ||
validate("strict.semiauto.band") | ||
testNoInstance("strict.semiauto.band", "Top") | ||
|
||
locally: | ||
import derivedInstances.* | ||
val instance = "derived.band" | ||
checkAll(s"$instance[Masked]]", tests[Masked].band) | ||
checkAll(s"$instance is Serializable", SerializableTests.serializable(Band[Masked])) | ||
|
||
end BandSuite | ||
|
||
object BandSuite: | ||
import ADTs.* | ||
|
||
object semiInstances: | ||
given Band[Masked[String]] = semiauto.band | ||
|
||
object strictInstances: | ||
given Band[Masked[String]] = strict.semiauto.band | ||
|
||
object derivedInstances: | ||
case class Masked(x: ADTs.Masked[String]) derives Band | ||
|
||
end BandSuite |
71 changes: 71 additions & 0 deletions
71
core/src/test/scala-3/cats/derived/BoundedSemilatticeSuite.scala
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,71 @@ | ||
/* | ||
* Copyright (c) 2016 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 cats.kernel.BoundedSemilattice | ||
import cats.kernel.laws.discipline.{BoundedSemilatticeTests, SerializableTests} | ||
|
||
import scala.compiletime.* | ||
|
||
class BoundedSemilatticeSuite extends KittensSuite: | ||
import ADTs.* | ||
import BoundedSemilatticeSuite.* | ||
|
||
inline def tests[A]: BoundedSemilatticeTests[A] = | ||
BoundedSemilatticeTests[A](using summonInline) | ||
|
||
inline def validate(inline instance: String): Unit = | ||
checkAll(s"$instance[Masked[String]]", tests[Masked[String]].boundedSemilattice) | ||
checkAll( | ||
s"$instance is Serializable", | ||
SerializableTests.serializable(summonInline[BoundedSemilattice[Masked[String]]]) | ||
) | ||
|
||
locally: | ||
import auto.boundedSemilattice.given | ||
validate("auto.boundedSemilattice") | ||
|
||
locally: | ||
import semiInstances.given | ||
validate("semiauto.boundedSemilattice") | ||
|
||
locally: | ||
import strictInstances.given | ||
validate("strict.semiauto.boundedSemilattice") | ||
testNoInstance("strict.semiauto.boundedSemilattice", "Top") | ||
|
||
locally: | ||
import derivedInstances.* | ||
val instance = "derived.boundedSemilattice" | ||
checkAll(s"$instance[Masked]]", tests[Masked].boundedSemilattice) | ||
checkAll(s"$instance is Serializable", SerializableTests.serializable(BoundedSemilattice[Masked])) | ||
|
||
end BoundedSemilatticeSuite | ||
|
||
object BoundedSemilatticeSuite: | ||
import ADTs.* | ||
|
||
object semiInstances: | ||
given BoundedSemilattice[Masked[String]] = semiauto.boundedSemilattice | ||
|
||
object strictInstances: | ||
given BoundedSemilattice[Masked[String]] = strict.semiauto.boundedSemilattice | ||
|
||
object derivedInstances: | ||
case class Masked(x: ADTs.Masked[String]) derives BoundedSemilattice | ||
|
||
end BoundedSemilatticeSuite |
Oops, something went wrong.