-
Notifications
You must be signed in to change notification settings - Fork 1
/
readme.txt
54 lines (32 loc) · 1.88 KB
/
readme.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
= 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:
https://github.com/zzorn/ScalaQuantity/blob/master/src/main/scala/scalaquantity/Units.scala
For more examples, see:
https://github.com/zzorn/ScalaQuantity/tree/master/src/test/scala/scalaquantity
=== 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.: http://jim-mcbeath.blogspot.com/2008/11/practical-church-numerals-in-scala.html
Contains some code adapted from the Metascala library (http://www.assembla.com/wiki/show/metascala) by Jesper Nordenberg.
The main difference of ScalaQuantity and Metascala is that ScalaQuantity focuses on just physical units.
== License ==
Licensed under the BSD license.