Implementing a Measured value in Scala -


a measured value consists of (typically nonnegative) floating-point number , unit-of-measure. point represent real-world quantities, , rules govern them. here's example:

scala> val oneinch = measure(1.0, inch) oneinch : measure[inch] = measure(1.0) scala> val twoinch = measure(2.0, inch) twoinch : measure[inch] = measure(2.0) scala> val onecm = measure(1.0, cm) onecm : measure[cm] = measure(1.0) scala> oneinch + twoinch res1: measure[inch] = measure(3.0) scala> oneinch + onecm res2: measure[inch] = measure(1.787401575) scala> onecm * onecm res3: measure[cmsq] = measure(1.0) scala> onecm * oneinch res4: measure[cmsq] = measure(2.54) scala> oncem * measure(1.0, liter) console>:7: error: conformance mismatch scala> oneinch * 2 == twoinch res5: boolean = true 

before excited, haven't implemented this, dummied repl session. i'm not sure of syntax, want able handle things adding measured quantities (even mixed units), multiplying measured quantities, , on, , ideally, scala's vaunted type-system guarantee @ compile-time expressions make sense.

my questions:

  1. is there extant terminology problem?
  2. has been done in scala?
  3. if not, how represent concepts "length" , "length measured in meters"?
  4. has been done in other language?

a $330-million mars probe lost because contractor using yards , pounds , nasa using meters , newtons. measure library have prevented crash.

well, functionality exists in java, meaning can use directly in scala.

jsr-275, moved google code. jscience implements spec. here's introduction. if want better interface, i'd use base , build wrapper around it.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -