Skip to content

Instantly share code, notes, and snippets.

@soc
Created October 21, 2011 16:46
Show Gist options
  • Save soc/1304298 to your computer and use it in GitHub Desktop.
Save soc/1304298 to your computer and use it in GitHub Desktop.
package metascala
object Units {
import Integers._
import Addables._
import Subtractables._
trait Unit {
type M <: MInt
type S <: MInt
}
final class TUnit[_M <: MInt, _S <: MInt] {
type M = _M
type S = _S
}
case class Quantity[M <: MInt, S <: MInt](value : Double) {
type This = Quantity[M, S]
def *[M2 <: MInt, S2 <: MInt](m : Quantity[M2, S2]) = Quantity[M + M2, S + S2](value * m.value)
def /[M2 <: MInt, S2 <: MInt](m : Quantity[M2, S2]) = Quantity[M - M2, S - S2](value / m.value)
def apply(v : Double) = Quantity[M, S](v * value)
}
val m = Quantity[_1, _0](1)
val s = Quantity[_0, _1](1)
type Length = Quantity[_1, _0]
type Time = Quantity[_0, _1]
type Area = Quantity[_2, _0]
type Volume = Quantity[_3, _0]
type Speed = Quantity[_1, _1#Neg]
type Acceleration = Quantity[_1, _2#Neg]
type Foo = Quantity[_1, _1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment