Skip to content

Instantly share code, notes, and snippets.

@soc
Created November 16, 2011 21:25
Show Gist options
  • Select an option

  • Save soc/1371465 to your computer and use it in GitHub Desktop.

Select an option

Save soc/1371465 to your computer and use it in GitHub Desktop.
case class Quantity[M <: MInt, KG <: MInt, S <: MInt, A <: MInt, K <: MInt, Mol <: MInt, CD <: MInt, T: Numeric](value: T) {
def asInt = Quantity[M, KG, S, A, K, Mol, CD, Int](numeric[T].toType[Int](value))
def asLong = Quantity[M, KG, S, A, K, Mol, CD, Long](numeric[T].toType[Long](value))
def asFloat = Quantity[M, KG, S, A, K, Mol, CD, Float](numeric[T].toType[Float](value))
def asDouble = Quantity[M, KG, S, A, K, Mol, CD, Double](numeric[T].toType[Double](value))
def toInt: Int = (numeric[T].toType[Int](value))
def toLong: Long = (numeric[T].toType[Long](value))
def toFloat: Float = (numeric[T].toType[Float](value))
def toDouble: Double = (numeric[T].toType[Double](value))
type This = Quantity[M, KG, S, A, K, Mol, CD, T]
def +(m: This) = Quantity[M, KG, S, A, K, Mol, CD, T](numeric[T].plus(value, m.value))
def -(m: This) = Quantity[M, KG, S, A, K, Mol, CD, T](numeric[T].minus(value, m.value))
def *[M2 <: MInt, KG2 <: MInt, S2 <: MInt, A2 <: MInt, K2 <: MInt, Mol2 <: MInt, CD2 <: MInt](m: Quantity[M2, KG2, S2, A2, K2, Mol2, CD2, T]) = Quantity[M + M2, KG + KG2, S + S2, A + A2, K + K2, Mol + Mol2, CD + CD2, T](numeric[T].times(value, m.value))
def /[M2 <: MInt, KG2 <: MInt, S2 <: MInt, A2 <: MInt, K2 <: MInt, Mol2 <: MInt, CD2 <: MInt](m: Quantity[M2, KG2, S2, A2, K2, Mol2, CD2, T]) = Quantity[M - M2, KG - KG2, S - S2, A - A2, K - K2, Mol - Mol2, CD - CD2, T](numeric[T].div(value, m.value))
}
implicit def measure[T: Numeric](v: T): Quantity[_0, _0, _0, _0, _0, _0, _0, T] = Quantity[_0, _0, _0, _0, _0, _0, _0, T](v)
implicit def numericToQuantity[T: Numeric](v: T): QuantityConstructor[T] = new QuantityConstructor[T](v)
class QuantityConstructor[T: Numeric](v: T) {
def m = Quantity[_1, _0, _0, _0, _0, _0, _0, T](v)
def kg = Quantity[_0, _1, _0, _0, _0, _0, _0, T](v)
def s = Quantity[_0, _0, _1, _0, _0, _0, _0, T](v)
def a = Quantity[_0, _0, _0, _1, _0, _0, _0, T](v)
def k = Quantity[_0, _0, _0, _0, _1, _0, _0, T](v)
def mol = Quantity[_0, _0, _0, _0, _0, _1, _0, T](v)
def cd = Quantity[_0, _0, _0, _0, _0, _0, _1, T](v)
def minute = Quantity[_0, _0, _1, _0, _0, _0, _0, T](numeric[T].times(v, numeric[T].fromInt(60)))
def hour = Quantity[_0, _0, _1, _0, _0, _0, _0, T](numeric[T].times(v, numeric[T].fromInt(3600)))
}
type Length[T] = Quantity[_1, _0, _0, _0, _0, _0, _0, T]
type Mass[T] = Quantity[_0, _1, _0, _0, _0, _0, _0, T]
type Time[T] = Quantity[_0, _0, _1, _0, _0, _0, _0, T]
type Currency[T] = Quantity[_0, _0, _0, _1, _0, _0, _0, T]
type Temperature[T] = Quantity[_0, _0, _0, _0, _1, _0, _0, T]
type Mol[T] = Quantity[_0, _0, _0, _0, _0, _1, _0, T]
type LuminousIntensity[T] = Quantity[_0, _0, _0, _0, _0, _0, _1, T]
type Area[T] = Quantity[_2, _0, _0, _0, _0, _0, _0, T]
type Volume[T] = Quantity[_3, _0, _0, _0, _0, _0, _0, T]
type Speed[T] = Quantity[_1, _0, _1#Neg, _0, _0, _0, _0, T]
type Acceleration[T] = Quantity[_1, _0, _2#Neg, _0, _0, _0, _0, T]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment