Realization of the measured value in Scala

A The measured value consists of a (usually non-negative) floating point number and unit. The point is to present the real quantities and rules that govern them. Here is an 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 you get too excited, I didn't implement this, I just started a REPL session. I'm not even sure of the syntax, I just want to be able to handle things like adding measured values ​​(even with mixed units), multiplying measured values, etc., And ideally I like the Scala vaunted type-system to guarantee at compile time that expressions make sense.

My questions:

  • Is there existing terminology for this problem?
  • Is it already done in Scala?
  • If not, how would I introduce concepts such as “length” and “length measured in meters”?
  • Was this done in some other language?

The forecast of $ 330 million of Mars was lost because the contractor used yards and pounds, and NASA used counters and newtons. A measurement library would prevent a failure.

+7
scala
source share
6 answers

Well, this functionality exists in Java, that is, you can use it directly in Scala.

jsr-275 which has been moved to google code . jscience implements the specification. Here is a good introduction . If you need a better interface, I would use it as a base and create a wrapper around it.

+5
source share

F # supports it; see, for example, this link for an introduction. Some work has been done in Scala on Units, for example here and. There is a Scala compiler plugin as described in this blog post . I briefly tried installing it, but using Scala 2.8.1, I got an exception when I started REPL, so I'm not sure if this plugin is currently active.

+11
source share

One question answers your question. You can thank me later.

Frink. http://futureboy.us/frinkdocs/

+5
source share

FYI, I developed the Scalar class in Scala to represent physical units. I currently use it for R&D work in air traffic control, and it works well for me. It does not check for consistency of elements at compile time, but it does at runtime. I have a unique scheme for easily replacing it with basic numeric types for efficiency after testing the application. Code and user manual can be found in

http://russp.us/scalar-scala.htm

Here is a summary from the site:

Summary - The Scala class was designed to represent physical scalars and eliminate errors associated with implicit physical units (such as confusing radians and degrees). Standard arithmetic operators are overloaded to provide syntax identical to the syntax for basic numeric types. The Scalar class itself does not define any units, but is part of a package that includes the full implementation of the standard metric unit and many common non-metric units. The scalar package also allows the user to define a specialized or reduced set of physical units for any particular application or domain. After the application has been developed and tested, the Scalar class can be disabled at compile time to achieve the efficiency of operations on basic numeric types, which are an order of magnitude faster. The scalar class can also be used for discrete units to provide integer type checking, thereby enhancing the Scala static type checking with additional dynamic type checking.

+1
source share

Let me clarify my previous post. I had to say: “These errors (“ meter / yard conversion errors ”) are automatically SELECTED (not“ processed ”), simply using my Scalar class. All unit conversions are done automatically. This is the easy part.

The more difficult part is checking for device inconsistencies, for example, adding length to speed. This raises the problem of dynamic and static type checking. I agree that a static check is usually preferable, but only if this can be done without sacrificing usability.

I have seen at least two “projects” for static checking of units, but I have never heard of anyone really using them for real work. If someone knows about the case when they were used, let me know. Until you use the software for real work, you do not know what problems will arise.

As I wrote above, I am currently using my Scalar class (http://russp.us/scalarscala.htm) to run R & D in ATC. I needed to make a lot of settings for convenience and convenience, but it works well for me. I would like to consider the static implementation of units if the proven goes, but at the moment I feel that I have essentially 99% of the cost of such a thing. Hey, the vast majority of scientists and engineers just use “Doubles,” so cut me a bit!

+1
source share

"Yes, ATC runtime checking software? I can see the headers now:" Flight 34 Brought Down by Meter / Yard Conversion. "

Sorry, but you don’t know what you are talking about. ATC software is tested for many years before it is deployed. This is enough time to catch unit mismatch errors.

More importantly, meter / yard conversions are not even a problem here. These errors are automatically handled by my Scalar class. For those kinds of errors, you do not need either static or dynamic validation. The question of static and dynamic verification arises only for block inconsistency, as in the case of adding time. Such errors are less common and usually fall with a dynamic check on the first test run.

By the way, the interface here is terrible.

0
source share

All Articles