title: Implementing a Measured value in Scala
body: <p>A Measured value consists of (typically nonnegative) floating-point number and unit-of-measure.  The point is to represent real-world quantities, and the rules that govern them.  Here's an example:</p>
  
  <pre><code>scala&gt; val oneinch = Measure(1.0, INCH)
  oneinch : Measure[INCH] = Measure(1.0)
  scala&gt; val twoinch = Measure(2.0, INCH)
  twoinch : Measure[INCH] = Measure(2.0)
  scala&gt; val onecm = Measure(1.0, CM)
  onecm : Measure[CM] = Measure(1.0)
  scala&gt; oneinch + twoinch
  res1: Measure[INCH] = Measure(3.0)
  scala&gt; oneinch + onecm
  res2: Measure[INCH] = Measure(1.787401575)
  scala&gt; onecm * onecm
  res3: Measure[CMSQ] = Measure(1.0)
  scala&gt; onecm * oneinch
  res4: Measure[CMSQ] = Measure(2.54)
  scala&gt; oncem * Measure(1.0, LITER)
  console&gt;:7: error: conformance mismatch
  scala&gt; oneinch * 2 == twoinch
  res5: Boolean = true
  </code></pre>
  
  <p>Before you get too excited, I haven't implemented this, I just dummied up a REPL session.  I'm not even sure of the syntax, I just want to be able to handle things like adding Measured quantities (even with mixed units), multiplying Measured quantities, and so on, and ideally, I like Scala's vaunted type-system to guarantee at compile-time that expressions make sense.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>Is there extant terminology for this problem?</li>
  <li>Has this already been done in Scala?</li>
  <li>If not, how would I represent concepts like "length" and "length measured in meters"?</li>
  <li>Has this been done in some other language?</li>
  </ol>
  
  <p><a href="http://en.wikipedia.org/wiki/Mars_Climate_Orbiter#The_metric.2FUS_customary_units_mix-up" rel="nofollow">A $330-million Mars probe</a> was lost because the contractor was using yards and pounds and NASA was using meters and newtons.  A Measure library would have prevented the crash.</p>