Skip to content

Instantly share code, notes, and snippets.

@ryoppy
Created December 14, 2014 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryoppy/35aba90718068e6b3cfc to your computer and use it in GitHub Desktop.
Save ryoppy/35aba90718068e6b3cfc to your computer and use it in GitHub Desktop.
単位変換する何かを作りたかったが、どうも微妙なのでボツ
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