-
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 NonEmptyAlternative on Scala 3
- Loading branch information
Showing
7 changed files
with
177 additions
and
40 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
46 changes: 46 additions & 0 deletions
46
core/src/main/scala-3/cats/derived/DerivedNonEmptyAlternative.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,46 @@ | ||
package cats.derived | ||
|
||
import cats.NonEmptyAlternative | ||
import shapeless3.deriving.K1 | ||
|
||
import scala.annotation.* | ||
import scala.compiletime.* | ||
|
||
@implicitNotFound("""Could not derive an instance of NonEmptyAlternative[F] where F = ${F}. | ||
Make sure that F[_] satisfies one of the following conditions: | ||
* it is a nested type [x] =>> G[H[x]] where G: NonEmptyAlternative and H: Applicative | ||
* it is a generic case class where all fields have a NonEmptyAlternative instance""") | ||
type DerivedNonEmptyAlternative[F[_]] = Derived[NonEmptyAlternative[F]] | ||
object DerivedNonEmptyAlternative: | ||
type Or[F[_]] = Derived.Or[NonEmptyAlternative[F]] | ||
|
||
@nowarn("msg=unused import") | ||
inline def apply[F[_]]: NonEmptyAlternative[F] = | ||
import DerivedNonEmptyAlternative.given | ||
summonInline[DerivedNonEmptyAlternative[F]].instance | ||
|
||
@nowarn("msg=unused import") | ||
inline def strict[F[_]]: NonEmptyAlternative[F] = | ||
import Strict.given | ||
summonInline[DerivedNonEmptyAlternative[F]].instance | ||
|
||
given nested[F[_], G[_]](using | ||
F: => Or[F], | ||
G: => DerivedApplicative.Or[G] | ||
): DerivedNonEmptyAlternative[[x] =>> F[G[x]]] = | ||
new Derived.Lazy(() => F.unify.compose(using G.unify)) with NonEmptyAlternative[[x] =>> F[G[x]]]: | ||
export delegate.* | ||
|
||
given product[F[_]](using inst: => K1.ProductInstances[Or, F]): DerivedNonEmptyAlternative[F] = | ||
Strict.product(using inst.unify) | ||
|
||
trait Product[T[f[_]] <: NonEmptyAlternative[f], F[_]](using K1.ProductInstances[T, F]) | ||
extends NonEmptyAlternative[F], | ||
DerivedApplicative.Product[T, F], | ||
DerivedSemigroupK.Product[T, F] | ||
|
||
object Strict: | ||
given product[F[_]](using K1.ProductInstances[NonEmptyAlternative, F]): DerivedNonEmptyAlternative[F] = | ||
new Product[NonEmptyAlternative, F] | ||
with DerivedApply.Product[NonEmptyAlternative, F] | ||
with DerivedSemigroupK.Product[NonEmptyAlternative, F] {} |
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
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
86 changes: 86 additions & 0 deletions
86
core/src/test/scala-3/cats/derived/NonEmptyAlternativeSuite.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,86 @@ | ||
/* | ||
* 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 cats.laws.discipline.* | ||
import cats.{Applicative, Monoid, NonEmptyAlternative} | ||
import org.scalacheck.Arbitrary | ||
import shapeless3.deriving.Const | ||
|
||
import scala.compiletime.* | ||
|
||
class NonEmptyAlternativeSuite extends KittensSuite: | ||
import ADTs.* | ||
import NonEmptyAlternativeSuite.* | ||
|
||
inline def tests[F[_]]: NonEmptyAlternativeTests[F] = | ||
NonEmptyAlternativeTests[F](summonInline) | ||
|
||
inline def validate(inline instance: String): Unit = | ||
checkAll(s"$instance[CaseClassWOption]", tests[CaseClassWOption].nonEmptyAlternative[Int, String, Long]) | ||
checkAll(s"$instance[OptList]", tests[OptList].nonEmptyAlternative[Int, String, Long]) | ||
checkAll(s"$instance[UnCons]", tests[UnCons].nonEmptyAlternative[Int, String, Long]) | ||
checkAll( | ||
s"$instance is Serializable", | ||
SerializableTests.serializable(summonInline[NonEmptyAlternative[CaseClassWOption]]) | ||
) | ||
|
||
locally: | ||
import auto.nonEmptyAlternative.given | ||
validate("auto.nonEmptyAlternative") | ||
|
||
locally: | ||
import semiInstances.given | ||
validate("semiauto.nonEmptyAlternative") | ||
|
||
locally: | ||
import strictInstances.given | ||
validate("strict.semiauto.nonEmptyAlternative") | ||
testNoInstance("strict.semiauto.nonEmptyAlternative", "TopK") | ||
|
||
locally: | ||
import derivedInstances.* | ||
val instance = "derived.nonEmptyAlternative" | ||
checkAll(s"$instance[CaseClassWOption]", tests[CaseClassWOption].nonEmptyAlternative[Int, String, Long]) | ||
checkAll(s"$instance[UnCons]", tests[UnCons].nonEmptyAlternative[Int, String, Long]) | ||
checkAll(s"$instance is Serializable", SerializableTests.serializable(Applicative[CaseClassWOption])) | ||
|
||
end NonEmptyAlternativeSuite | ||
|
||
object NonEmptyAlternativeSuite: | ||
import ADTs.* | ||
|
||
type OptList[A] = Option[List[A]] | ||
type UnCons[+A] = (Option[A], Vector[A]) | ||
|
||
object semiInstances: | ||
given NonEmptyAlternative[CaseClassWOption] = semiauto.nonEmptyAlternative | ||
given NonEmptyAlternative[OptList] = semiauto.nonEmptyAlternative | ||
given NonEmptyAlternative[UnCons] = semiauto.nonEmptyAlternative | ||
|
||
object strictInstances: | ||
given [T: Monoid]: NonEmptyAlternative[Const[T]] = semiauto.nonEmptyAlternative | ||
given [F[_]: NonEmptyAlternative, G[_]: Applicative]: NonEmptyAlternative[[x] =>> F[G[x]]] = | ||
NonEmptyAlternative[F].compose[G] | ||
given NonEmptyAlternative[CaseClassWOption] = strict.semiauto.nonEmptyAlternative | ||
given NonEmptyAlternative[UnCons] = strict.semiauto.nonEmptyAlternative | ||
|
||
object derivedInstances: | ||
case class CaseClassWOption[A](x: ADTs.CaseClassWOption[A]) derives NonEmptyAlternative | ||
case class UnCons[A](x: NonEmptyAlternativeSuite.UnCons[A]) derives NonEmptyAlternative | ||
|
||
end NonEmptyAlternativeSuite |