Skip to content

Commit

Permalink
Avoid implicit name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
RustedBones committed May 27, 2024
1 parent 11d4bd9 commit 889d14f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import org.scalacheck.{Arbitrary, Cogen}

import scala.reflect.macros.*
object ScalaCheckMacros {
def genArbitraryMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
def autoDerivationArbitrary[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
import c.universe._
val wtt = weakTypeTag[T]
q"""_root_.magnolify.scalacheck.ArbitraryDerivation.gen[$wtt]"""
}

def genCogenMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
def autoDerivationCogen[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = {
import c.universe._
val wtt = weakTypeTag[T]
q"""_root_.magnolify.scalacheck.CogenDerivation.gen[$wtt]"""
Expand All @@ -35,6 +35,7 @@ object ScalaCheckMacros {
}

trait AutoDerivations {
implicit def genArbitrary[T]: Arbitrary[T] = macro ScalaCheckMacros.genArbitraryMacro[T]
implicit def genCogen[T]: Cogen[T] = macro ScalaCheckMacros.genCogenMacro[T]
implicit def autoDerivationArbitrary[T]: Arbitrary[T] =
macro ScalaCheckMacros.autoDerivationArbitrary[T]
implicit def autoDerivationCogen[T]: Cogen[T] = macro ScalaCheckMacros.autoDerivationCogen[T]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.scalacheck.{Arbitrary, Cogen}
import scala.deriving.Mirror

trait AutoDerivations:
inline implicit def genArbitrary[T](using Mirror.Of[T]): Arbitrary[T] =
inline implicit def autoDerivationArbitrary[T](using Mirror.Of[T]): Arbitrary[T] =
ArbitraryDerivation.derivedMirror[T]
inline implicit def genCogen[T](using Mirror.Of[T]): Cogen[T] =
inline implicit def autoDerivationCogen[T](using Mirror.Of[T]): Cogen[T] =
CogenDerivation.derivedMirror[T]
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ package object semiauto {
@deprecated("Use Gogen.gen[T] instead", "0.7.0")
val CogenDerivation = magnolify.scalacheck.CogenDerivation

implicit def genArbitrary(a: Arbitrary.type): magnolify.scalacheck.ArbitraryDerivation.type =
implicit def semiautoDerivationArbitrary(
a: Arbitrary.type
): magnolify.scalacheck.ArbitraryDerivation.type =
magnolify.scalacheck.ArbitraryDerivation
implicit def genCogen(c: Cogen.type): magnolify.scalacheck.CogenDerivation.type =
implicit def semiautoDerivationCogen(c: Cogen.type): magnolify.scalacheck.CogenDerivation.type =
magnolify.scalacheck.CogenDerivation
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import scala.reflect._

class ArbitraryDerivationSuite extends MagnolifySuite {
import TestArbitrary.arbSeed
import magnolify.scalacheck.auto.genArbitrary
import magnolify.scalacheck.auto.autoDerivationArbitrary

private def test[T: ClassTag](implicit t: Arbitrary[T]): Unit = {
// TODO val g = ensureSerializable(t).arbitrary
Expand Down Expand Up @@ -73,8 +73,9 @@ class ArbitraryDerivationSuite extends MagnolifySuite {

// magnolia scala3 limitation:
// For a recursive structures it is required to assign the derived value to an implicit variable
implicit val arbNode: Arbitrary[Node] = genArbitrary
implicit val arbGNode: Arbitrary[GNode[Int]] = genArbitrary
import magnolify.scalacheck.semiauto.semiautoDerivationArbitrary
implicit val arbNode: Arbitrary[Node] = Arbitrary.gen
implicit val arbGNode: Arbitrary[GNode[Int]] = Arbitrary.gen

test[Node]
test[GNode[Int]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import scala.reflect.*

class CogenDerivationSuite extends MagnolifySuite {
import TestArbitrary.arbSeed
import magnolify.scalacheck.auto.genCogen
import magnolify.scalacheck.auto.autoDerivationCogen

private def test[T: ClassTag](implicit arb: Arbitrary[T], t: Cogen[T]): Unit = {
// TODO val co = ensureSerializable(t)
Expand Down Expand Up @@ -60,8 +60,9 @@ class CogenDerivationSuite extends MagnolifySuite {

// magnolia scala3 limitation:
// For a recursive structures it is required to assign the derived value to an implicit variable
implicit val cogenNode: Cogen[Node] = genCogen
implicit val cogenGNode: Cogen[GNode[Int]] = genCogen
import magnolify.scalacheck.semiauto.semiautoDerivationCogen
implicit val cogenNode: Cogen[Node] = Cogen.gen
implicit val cogenGNode: Cogen[GNode[Int]] = Cogen.gen

test[Node]
test[GNode[Int]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import org.scalacheck._
import scala.reflect._

class FunctionDerivationSuite extends MagnolifySuite {
import magnolify.scalacheck.auto.genCogen
import magnolify.scalacheck.auto.genArbitrary
import magnolify.scalacheck.auto._

private def test[A: ClassTag, B: ClassTag](implicit
t: Arbitrary[A => B],
Expand Down

0 comments on commit 889d14f

Please sign in to comment.