Created
October 3, 2011 19:15
-
-
Save soc/1259956 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
package metascala | |
object Units { | |
import Integers._ | |
import Utils._ | |
import Addables._ | |
import Subtractables._ | |
trait Unit { | |
type M <: MInt | |
type KG <: MInt | |
type S <: MInt | |
type A <: MInt | |
type K <: MInt | |
type Mol <: MInt | |
type CD <: MInt | |
} | |
final class TUnit[_M <: MInt, _KG <: MInt, _S <: MInt, _A <: MInt, _K <: MInt, _Mol <: MInt, _CD <: MInt] { | |
type M = _M | |
type KG = _KG | |
type S = _S | |
type A = _A | |
type K = _K | |
type Mol = _Mol | |
type CD = _CD | |
} | |
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