Skip to content

Commit

Permalink
De-prioritize intWitnessAs
Browse files Browse the repository at this point in the history
  • Loading branch information
armanbilge committed Dec 19, 2024
1 parent 20df7cc commit 0be209c
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,15 @@ import scala.compiletime.{constValue, error}
final case class WitnessAs[A, B](fst: A, snd: B)

object WitnessAs extends WitnessAs1 {
def apply[A, B](implicit ev: WitnessAs[A, B]): WitnessAs[A, B] = ev

implicit def intWitnessAs[B, A <: Int](implicit
wa: ValueOf[A],
ta: ToInt[A],
nb: Numeric[B]
): WitnessAs[A, B] =
WitnessAs(wa.value, nb.fromInt(ta.apply()))
def apply[A, B](using ev: WitnessAs[A, B]): WitnessAs[A, B] = ev

inline given singletonWitnessAs[B, A <: B]: WitnessAs[A, B] = {
inline val a = constValue[A]
WitnessAs(a, a)
}
}

trait WitnessAs1 {
trait WitnessAs1 extends WitnessAs2 {
inline given intWitnessAsByte[A <: Int]: WitnessAs[A, Byte] =
inline constValue[A] match {
case a if a >= -128 && a <= 127 => WitnessAs(a, a.toByte)
Expand Down Expand Up @@ -89,3 +82,12 @@ trait WitnessAs1 {
WitnessAs(a, BigDecimal(a))
}
}

trait WitnessAs2 {
given intWitnessAs[B, A <: Int](using
wa: ValueOf[A],
ta: ToInt[A],
nb: Numeric[B]
): WitnessAs[A, B] =
WitnessAs(wa.value, nb.fromInt(ta.apply()))
}

0 comments on commit 0be209c

Please sign in to comment.