public
Created

  • Download Gist
weak-conformance.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
sealed trait WeakConformance[A <: AnyVal, B <: AnyVal, C] {
implicit def aToC(a: A): C
 
implicit def bToC(b: B): C
}
 
object WeakConformance {
implicit def SameSame[T <: AnyVal]: WeakConformance[T, T, T] = new WeakConformance[T, T, T] {
implicit def aToC(a: T): T = a
 
implicit def bToC(b: T): T = b
}
 
implicit def IntDouble: WeakConformance[Int, Double, Double] = new WeakConformance[Int, Double, Double] {
implicit def aToC(a: Int) = a
 
implicit def bToC(b: Double) = b
}
 
implicit def DoubleInt: WeakConformance[Double, Int, Double] = new WeakConformance[Double, Int, Double] {
implicit def aToC(a: Double) = a
 
implicit def bToC(b: Int) = b
}
 
// etc
}
 
 
def unify[A <: AnyVal, B <: AnyVal, C](a: A, b: B)(implicit ev: WeakConformance[A, B, C]): (C, C) = {
import ev._
(a: C, b: C)
}
 
unify(1, 1): (Int, Int)
unify(1, 1.0): (Double, Double)
unify(1.0, 1): (Double, Double)
unify(1.0, 1.0): (Double, Double)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.