Skip to content

Commit

Permalink
fix: Ambiguous implicit when both Eq[A] and Hash[A] are available (
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltotore authored Aug 1, 2023
1 parent 1c8918e commit 7651ed6
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cats/src/io/github/iltotore/iron/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import _root_.cats.{Eq, Monoid, Order, Show}
import _root_.cats.data.Validated.{Invalid, Valid}
import io.github.iltotore.iron.constraint.numeric.{Greater, Less, Positive, Negative}

import scala.util.NotGiven

object cats extends IronCatsInstances:

/**
Expand Down Expand Up @@ -169,11 +171,13 @@ object cats extends IronCatsInstances:
* Represent all Cats' typeclass instances for Iron.
*/
private trait IronCatsInstances extends IronCatsLowPriority, RefinedTypeOpsCats:
inline given [A, C](using inline ev: Eq[A]): Eq[A :| C] = ev.asInstanceOf[Eq[A :| C]]

inline given [A, C](using inline ev: Order[A]): Order[A :| C] = ev.asInstanceOf[Order[A :| C]]
//The `NotGiven` implicit parameter is mandatory to avoid ambiguous implicit error when both Eq[A] and Hash[A]/PartialOrder[A] exist
inline given [A, C](using inline ev: Eq[A], notHashOrOrder: NotGiven[Hash[A] | PartialOrder[A]]): Eq[A :| C] = ev.asInstanceOf[Eq[A :| C]]

inline given [A, C](using inline ev: PartialOrder[A]): PartialOrder[A :| C] = ev.asInstanceOf[PartialOrder[A :| C]]
inline given [A, C](using inline ev: PartialOrder[A], notOrder: NotGiven[Order[A]]): PartialOrder[A :| C] = ev.asInstanceOf[PartialOrder[A :| C]]

inline given [A, C](using inline ev: Order[A]): Order[A :| C] = ev.asInstanceOf[Order[A :| C]]

inline given [A, C](using inline ev: Show[A]): Show[A :| C] = ev.asInstanceOf[Show[A :| C]]

Expand Down Expand Up @@ -217,6 +221,7 @@ private trait IronCatsInstances extends IronCatsLowPriority, RefinedTypeOpsCats:
* Cats' instances for Iron that need to have a lower priority to avoid ambiguous implicits.
*/
private trait IronCatsLowPriority:

inline given [A, C](using inline ev: Hash[A]): Hash[A :| C] = ev.asInstanceOf[Hash[A :| C]]

private trait RefinedTypeOpsCats extends RefinedTypeOpsCatsLowPriority:
Expand Down

0 comments on commit 7651ed6

Please sign in to comment.