Created
October 3, 2011 02:28
-
-
Save soc/1258307 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Quantity[M <: MInt, KG <: MInt, S <: MInt, A <: MInt, K <: MInt, Mol <: MInt, CD <: MInt, T : Numeric](value: T) { | |
type This = Quantity[M, KG, S, A, K, Mol, CD, T] | |
def +(m: This) = Quantity[M, KG, S, A, K, Mol, CD, T](implicitly[Numeric[T]].plus(value, m.value)) | |
def -(m: This) = Quantity[M, KG, S, A, K, Mol, CD, T](implicitly[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](implicitly[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](implicitly[Numeric[T]].times(value, m.value)) | |
def apply(v: T) = Quantity[M, KG, S, A, K, Mol, CD, T](implicitly[Numeric[T]].times(v, value)) | |
} | |
implicit def measure[T : Numeric](v: T) = Quantity[_0, _0, _0, _0, _0, _0, _0, T](v) | |
implicit def numericToQuantity[T : Numeric](v: 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) | |
} | |
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