Created
May 24, 2010 19:36
-
-
Save retronym/412321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment