Created
December 14, 2014 11:44
-
-
Save ryoppy/35aba90718068e6b3cfc 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
case class Base(n: BigDecimal) { | |
def toMicro: Micro = Micro(n * 1000000) | |
} | |
case class Micro(n: BigDecimal) { | |
def toBase: Base = Base(n / 1000000) | |
} | |
sealed abstract class Converter[From, To] extends (From => To) | |
object Converter { | |
def apply[A, B](a: A)(implicit c: Converter[A, B]): B = c(a) | |
implicit val base2micro = new Converter[Base, Micro] { def apply(a: Base): Micro = a.toMicro } | |
implicit val micro2base = new Converter[Micro, Base] { def apply(a: Micro): Base = a.toBase } | |
} | |
locally { | |
val from = Micro(1000000) | |
val to = Converter[Micro, Base](from) | |
println(s"${from} => ${to}") | |
} | |
locally { | |
val from = Base(1) | |
val to = Converter[Base, Micro](from) | |
println(s"${from} => ${to}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment