Skip to content

Commit

Permalink
Replace implicits by givens
Browse files Browse the repository at this point in the history
  • Loading branch information
cquiroz committed May 24, 2024
1 parent bf181ff commit 8499bb5
Show file tree
Hide file tree
Showing 41 changed files with 83 additions and 94 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ThisBuild / tlBaseVersion := "0.97"
ThisBuild / tlBaseVersion := "0.98"
ThisBuild / tlCiReleaseBranches := Seq("master")
ThisBuild / githubWorkflowEnv += "MUNIT_FLAKY_OK" -> "true"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected[data] trait ZipperFactory[Z[A] <: ZipperOps[A, Zipper[A]]] {
/**
* @group Typeclass Instances
*/
implicit def equal[A: Eq]: Eq[Z[A]] =
given [A: Eq]: Eq[Z[A]] =
Eq.instance { (a, b) =>
a.focus === b.focus && a.lefts === b.lefts && a.rights === b.rights
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ object Band {
fromTag(s).getOrElse(throw new NoSuchElementException(s"Band: Invalid tag: '$s'"))

/** @group Typeclass Instances */
implicit val BandEnumerated: Enumerated[Band] =
given Enumerated[Band] =
new Enumerated[Band] {
def all = Band.all
def tag(a: Band) = a.tag
Expand All @@ -341,7 +341,7 @@ object Band {
Order.by(_.center)

/** @group Typeclass Instances */
implicit val BandOrdering: Ordering[Band] =
BandEnumerated.toOrdering
given Ordering[Band] =
Enumerated[Band].toOrdering

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Breakpoint {
/** @group Constructors */ case object Enabled extends Breakpoint("enabled")
/** @group Constructors */ case object Disabled extends Breakpoint("disabled")

implicit val BreakpointEnumerated: Enumerated[Breakpoint] =
given Enumerated[Breakpoint] =
Enumerated.from(Enabled, Disabled).withTag(_.tag)

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object GmosAdc {
fromTag(s).getOrElse(throw new NoSuchElementException(s"GmosAdc: Invalid tag: '$s'"))

/** @group Typeclass Instances */
implicit val GmosAdcEnumerated: Enumerated[GmosAdc] =
given Enumerated[GmosAdc] =
new Enumerated[GmosAdc] {
def all = GmosAdc.all
def tag(a: GmosAdc) = a.tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object GnirsCamera {
fromTag(s).getOrElse(throw new NoSuchElementException(s"GnirsCamera: Invalid tag: '$s'"))

/** @group Typeclass Instances */
implicit val GnirsCameraEnumerated: Enumerated[GnirsCamera] =
given Enumerated[GnirsCamera] =
new Enumerated[GnirsCamera] {
def all = GnirsCamera.all
def tag(a: GnirsCamera) = a.tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object GpiASU {
fromTag(s).getOrElse(throw new NoSuchElementException(s"GpiASU: Invalid tag: '$s'"))

/** @group Typeclass Instances */
implicit val GpiASUEnumerated: Enumerated[GpiASU] =
given Enumerated[GpiASU] =
new Enumerated[GpiASU] {
def all = GpiASU.all
def tag(a: GpiASU) = a.tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object ImageQuality {
case object OnePointFive extends ImageQuality("one_point_five", 15.withRefinedUnit[Positive, DeciArcSecond])
case object TwoPointZero extends ImageQuality("two_point_zero", 20.withRefinedUnit[Positive, DeciArcSecond])

implicit val ImageQualityEnumerated: Enumerated[ImageQuality] =
given Enumerated[ImageQuality] =
Enumerated.from(
PointOne,
PointTwo,
Expand All @@ -48,6 +48,6 @@ object ImageQuality {
TwoPointZero
).withTag(_.tag)

implicit val ImageQualityDisplay: Display[ImageQuality] =
given Display[ImageQuality] =
Display.byShortName(_.label)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ object WaterVapor {
case object Median extends WaterVapor("median", "Median")
case object Wet extends WaterVapor("wet", "Wet")

implicit val WaterVaporEnumerated: Enumerated[WaterVapor] =
given Enumerated[WaterVapor] =
Enumerated.from(VeryDry, Dry, Median, Wet).withTag(_.tag)

implicit val WatorVaporDisplay: Display[WaterVapor] =
given Display[WaterVapor] =
Display.byShortName(_.label)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ object Index extends IndexOptics {
val One: Index =
fromShort.unsafeGet(1)

implicit val OrderIndex: Order[Index] =
given Order[Index] =
Order.by(_.toShort)

implicit val OrderingIndex: scala.math.Ordering[Index] =
OrderIndex.toOrdering
given scala.math.Ordering[Index] =
summon[Order[Index]].toOrdering

implicit val showIndex: Show[Index] =
given Show[Index] =
Show.fromToString

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ object RadialVelocity {
val Zero: RadialVelocity = new RadialVelocity(0.withUnit[MetersPerSecond])

/** @group Typeclass Instances */
implicit val orderRadialVelocity: Order[RadialVelocity] =
given Order[RadialVelocity] =
Order.by(_.rv)

/** @group Typeclass Instances */
implicit val showRadialVelocity: Show[RadialVelocity] =
given Show[RadialVelocity] =
Show.fromToString

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ object Redshift {
val redshift: Iso[BigDecimal, Redshift] = Iso(Redshift.apply)(_.z)

/** @group Typeclass Instances */
implicit val orderRedshift: Order[Redshift] =
given Order[Redshift] =
Order.by(_.z)

/** @group Typeclass Instances */
implicit val showRedshift: Show[Redshift] =
given Show[Redshift] =
Show.fromToString

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ final case class OrcidProfile(
}

object OrcidProfile {
implicit val eqOrcidProfile: Eq[OrcidProfile] =
given Eq[OrcidProfile] =
Eq.by(x => (x.orcidId, x.givenName, x.familyName, x.creditName, x.primaryEmail))
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ object PosAngleConstraint extends PosAngleConstraintOptics {

val Default: PosAngleConstraint = Fixed(Angle.Angle0)

implicit val eqPosAngle: Eq[PosAngleConstraint] = Eq.instance {
given Eq[PosAngleConstraint] = Eq.instance {
case (Fixed(a), Fixed(b)) => a === b
case (AllowFlip(a), AllowFlip(b)) => a === b
case (AverageParallactic, AverageParallactic) => true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ case object GuestRole extends Role(Access.Guest)
case class ServiceRole(serviceName: String) extends Role(Access.Service, Some(serviceName))

object ServiceRole {
implicit val eqServiceRole: Eq[ServiceRole] = Eq.by(_.serviceName)
given Eq[ServiceRole] = Eq.by(_.serviceName)
}

/** The class of roles taken on by authenticated users. */
Expand All @@ -60,7 +60,7 @@ object StandardRole extends WithGid('r'.refined) {
/** The `Admin` role is a superuser role that allows access to all functionality. */
final case class Admin(id: StandardRole.Id) extends StandardRole(Access.Admin)

implicit val eqStandardRole: Eq[StandardRole] = Eq.instance {
given Eq[StandardRole] = Eq.instance {
case (Pi(a), Pi(b)) => a === b
case (Ngo(a, b), Ngo(c, d)) => a === c && b === d
case (Staff(a), Staff(b)) => a === b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object StepConfig {
}

object Gcal {
implicit val eqStepConfigGcal: Eq[Gcal] =
given Eq[Gcal] =
Eq.by(x => (x.lamp, x.filter, x.diffuser, x.shutter))

opaque type Lamp = Either[GcalContinuum, NonEmptySet[GcalArc]]
Expand Down Expand Up @@ -91,7 +91,7 @@ object StepConfig {
case (_, a :: as) => NonEmptySet.of(a, as*).asRight[GcalContinuum].asRight
}

implicit val eqLamp: Eq[Lamp] =
given Eq[Lamp] =
Eq.by { lamp => (lamp.continuum, lamp.arcs) }

}
Expand Down Expand Up @@ -124,7 +124,7 @@ object StepConfig {
final case class Science(offset: Offset, guiding: StepGuideState) extends StepConfig(StepType.Science)

object Science {
implicit val eqStepConfigScience: Eq[Science] =
given Eq[Science] =
Eq.by { a => (
a.offset,
a.guiding
Expand All @@ -143,15 +143,15 @@ object StepConfig {

object SmartGcal {

implicit val EqSmartGcal: Eq[SmartGcal] =
given Eq[SmartGcal] =
Eq.by(_.smartGcalType)

val smartGcalType: Lens[SmartGcal, SmartGcalType] =
Focus[SmartGcal](_.smartGcalType)

}

implicit val eqStepConfig: Eq[StepConfig] = Eq.instance {
given Eq[StepConfig] = Eq.instance {
case (Bias, Bias) => true
case (Dark, Dark) => true
case (a @ Gcal(_, _, _, _), b @ Gcal(_, _, _, _)) => a === b
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final case class SplitEpi[A, B](get: A => B, reverseGet: B => A) {
reverseGet(get(a))

/** If we can reverseGet a Product as a String we can implement a tagged toString like "Foo(stuff)". */
def productToString(b: B)(implicit
def productToString(b: B)(using
as: A =:= String,
bp: B <:< Product
): String =
Expand All @@ -86,7 +86,7 @@ final case class SplitEpi[A, B](get: A => B, reverseGet: B => A) {
* If we provide a tag like "Foo" and reverseGet as a String we can implement a nice toString like
* "Foo(stuff)".
*/
def taggedToString(tag: String, b: B)(implicit
def taggedToString(tag: String, b: B)(using
as: A =:= String
): String =
new StringBuilder(tag)
Expand All @@ -104,7 +104,7 @@ object SplitEpi {
SplitEpi(p.get, p.reverseGet)

/** SplitEpi forms a category. */
implicit def SplitEpiCategory: Category[SplitEpi] =
given Category[SplitEpi] =
new Category[SplitEpi] {
def id[A]: SplitEpi[A, A] = SplitEpi(identity, identity)
def compose[A, B, C](f: SplitEpi[B, C], g: SplitEpi[A, B]): SplitEpi[A, C] = g.andThen(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ object SplitMono {
SplitMono(p.get, p.reverseGet)

/** SplitMono forms a category. */
implicit def SplitMonoCategory: Category[SplitMono] =
given Category[SplitMono] =
new Category[SplitMono] {
def id[A]: SplitMono[A, A] = SplitMono(identity, identity)
def compose[A, B, C](f: SplitMono[B, C], g: SplitMono[A, B]): SplitMono[A, C] = g.andThen(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object Wedge {
Wedge(p.get, p.reverseGet)

/** Wedge forms a category. */
implicit def WedgeCategory: Category[Wedge] =
given Category[Wedge] =
new Category[Wedge] {
def id[A]: Wedge[A, A] = Wedge(identity, identity)
def compose[A, B, C](f: Wedge[B, C], g: Wedge[A, B]): Wedge[A, C] = g.andThen(f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ package lucuma.core.syntax

import scala.collection.immutable.TreeSet

final class TreeSetCompanionOps(private val self: TreeSet.type) extends AnyVal {

/** Creates a `TreeSet` from a `List`, provided an `Ordering` is available. */
def fromList[A: Ordering](lst: List[A]): TreeSet[A] =
TreeSet(lst*)

}

trait ToTreeSetCompanionOps {
implicit def ToTreeSetCompanionOps(c: TreeSet.type): TreeSetCompanionOps =
new TreeSetCompanionOps(c)
extension(c: TreeSet.type)
/** Creates a `TreeSet` from a `List`, provided an `Ordering` is available. */
def fromList[A: Ordering](lst: List[A]): TreeSet[A] =
TreeSet(lst*)
}

object treesetcompanion extends ToTreeSetCompanionOps
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait Display[A] {
}

object Display {
def apply[A](implicit ev: Display[A]): ev.type = ev
def apply[A](using ev: Display[A]): ev.type = ev

/**
* Create an instance of `Display` using the provided functions.
Expand Down Expand Up @@ -43,7 +43,7 @@ object Display {
def shortName(a: A) = toShortName(a)
}

implicit val contravariantDisplay: Contravariant[Display] = new Contravariant[Display] {
given Contravariant[Display] = new Contravariant[Display] {
def contramap[A, B](fa: Display[A])(f: B => A): Display[B] =
new Display[B] {
def shortName(b: B) = fa.shortName(f(b))
Expand Down
12 changes: 6 additions & 6 deletions modules/testkit/src/main/scala/lucuma/core/arb/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.scalacheck.Arbitrary.arbitrary

import scala.collection.immutable.TreeMap

implicit class MoreGenOps[A](g: Gen[A]) {
extension[A](g: Gen[A]) {

/** Like `retryUntil` but retries until the specified PartialFunction is defined. */
def collectUntil[B](f: PartialFunction[A, B]): Gen[B] =
Expand All @@ -25,16 +25,16 @@ implicit class MoreGenOps[A](g: Gen[A]) {
}

// This isn't in scalacheck for whatever reason
implicit def mapCogen[A: Cogen, B: Cogen]: Cogen[Map[A, B]] =
given [A: Cogen, B: Cogen]: Cogen[Map[A, B]] =
Cogen[List[(A, B)]].contramap(_.toList)

// The above doesn't seem to match a TreeMap unless explicitly cast to a Map
implicit def treeMapCogen[A: Cogen, B: Cogen]: Cogen[TreeMap[A, B]] =
given [A: Cogen, B: Cogen]: Cogen[TreeMap[A, B]] =
Cogen[Map[A, B]].contramap(_.toMap)

// This doesn't seem to exist anywhere? https://github.com/non/cats-check
// would be useful. All we need is `Applicative` for now though I suppose.
implicit val applicativeGen: Applicative[Gen] = new Applicative[Gen] {
given Applicative[Gen] = new Applicative[Gen] {
def ap[A, B](gf: Gen[A => B])(ga: Gen[A]): Gen[B] =
for {
f <- gf
Expand All @@ -45,9 +45,9 @@ implicit val applicativeGen: Applicative[Gen] = new Applicative[Gen] {
Gen.const(a)
}

implicit val cogenNonNegativeInt: Cogen[NonNegInt] = Cogen[Int].contramap(_.value)
given Cogen[NonNegInt] = Cogen[Int].contramap(_.value)

implicit val cogenNonEmptyString: Cogen[NonEmptyString] = Cogen[String].contramap(_.value)
given Cogen[NonEmptyString] = Cogen[String].contramap(_.value)

def newTypeArbitrary[T: Arbitrary](base: NewType[T]): Arbitrary[base.Type] =
Arbitrary(arbitrary[T].map(base.apply(_)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import org.scalacheck.Cogen
import org.scalacheck.Gen.*

trait ArbEnumZipper {
implicit def arbEnumZipper[A: Enumerated]: Arbitrary[EnumZipper[A]] =
given [A: Enumerated]: Arbitrary[EnumZipper[A]] =
Arbitrary(
for {
z <- const(EnumZipper.of[A])
a <- arbitrary[A]
} yield z.withFocus(a)
)

implicit def enumZipperCogen[A: Enumerated]: Cogen[EnumZipper[A]] =
given [A: Enumerated]: Cogen[EnumZipper[A]] =
Cogen[A].contramap(_.focus)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import org.scalacheck.Cogen

trait ArbApparentRadialVelocity {

implicit val arbApparentRadialVelocity: Arbitrary[ApparentRadialVelocity] =
given Arbitrary[ApparentRadialVelocity] =
Arbitrary {
for {
cz <- arbitrary[BigDecimal]
} yield ApparentRadialVelocity(cz.withUnit[MetersPerSecond])
}

implicit val cogApparentRadialVelocity: Cogen[ApparentRadialVelocity] =
given Cogen[ApparentRadialVelocity] =
Cogen[BigDecimal].contramap(_.cz.value)
}

Expand Down
Loading

0 comments on commit 8499bb5

Please sign in to comment.