Skip to content
This repository

Library for compile time checked SI quantities and units of measurement in Scala.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 src
Octocat-spinner-32 .gitignore
Octocat-spinner-32 license-metascala.txt
Octocat-spinner-32 license-scalaquantity.txt
Octocat-spinner-32 pom.xml
Octocat-spinner-32 readme.txt
= ScalaQuantity =

Library for expressing quantities and units of measurement in Scala in a type safe manner, where the compiler checks that assignments and calculations with the units are correct.
The unit checking is done at compile time, quantities compile down to a simple wrapper class around a Double.

== Usage ==

import scalaquantity.Units._

object VelociraptorTest {
  def main(args: Array[String]) {

    val velociraptorSpeed = 25 * m / s
    val humanRunningSpeed = 10 * km / h
    val distance = 30 * feet

    val humanRemainingLifeTime: Time = distance / (velociraptorSpeed - humanRunningSpeed)

    println("Seconds left to live: " + humanRemainingLifeTime)

For available units, see:

For more examples, see:

=== Gotchas ===
IDE / Compile type errors will not be very readable, as ScalaQuantity is using type magic for the compile-time validation.  
On the other hand, the errors will be caught at compile and not runtime.

You may need to parenthesize some expressions more than would first be intuitive, e.g.
val speed: Speed = 100 * miles / 2 * hours   // Compile error: Will evaluate to 50 mile hours, instead of 50 miles per hour.
val speed: Speed = 100 * miles / (2 * hours) // Compiles ok, speed is 50 miles/hour.

== Credits ==

Author: Hans Häggström, zzorn at iki dot fi

Based on and inspired by existing code for Church Numerals in Scala.
See e.g.:

Contains some code adapted from the Metascala library ( by Jesper Nordenberg.
The main difference of ScalaQuantity and Metascala is that ScalaQuantity focuses on just physical units. 

== License ==

Licensed under the BSD license.
Something went wrong with that request. Please try again.