Question

I was reading Andrew Kennedy's blog post series on units of measurement in F# and it makes a lot of sense in a lot of cases. Are there any other languages that have such a system?

Edit: To be more clear, I mean the flexible units of measurement system where you can define your own arbitrarily.

Was it helpful?

Solution

Does TI-89 BASIC count? Enter 54_kg * (_c^2) and it will give you an answer in joules.

Other than that, I can't recall any languages that have it built in, but any language with decent OO should make it simple to roll your own. Which means someone else probably already did.

Google confirms. For example, here's one in Python. __repr__ could easily be amended to also select the most appropriate derived unit, etc.

CPAN has several modules for Perl: Physics::Unit, Data::Dimensions, Class::Measure, Math::Units::PhysicalValue, and a handful of others that will convert but don't really combine values with units.

OTHER TIPS

C++ has it, in the form of boost::units.

I'm not sure if this really counts, but the RPL system on my HP-48 calculator does have similar features. I can write 40_gal 5_l + and get the right answer of 156.416 liters.

I believe I saw that Fortress support this, I'll see if I can find a link.

I can't find a specific link, but the language specification makes mention of it in a couple of places. The 1.0 language specification also says that dimensions and units were temporarily dropped from the specification (along with a whole heap of other features) to match up with the current implementation. It's a work in progress, so I guess things are in flux.

F# is the first mainstream language to support this feature.

There is also a Java specification for units at http://jcp.org/en/jsr/detail?id=275 and you can already use it from here http://jscience.org/

Nemerle has something much better than F# !

You should check this one : http://rsdn.ru/forum/src/1823225.flat.aspx#1823225 .

It is really great . And you can download here : http://rsdn.ru/File/27948/Oyster.Units.0.06.zip

Some example:

def m3 = 1 g;
def m4 = Si.Mass(m1);

WriteLine($"Mass in SI: $m4, in CGS: $m3");

def x1 = Si.Area(1 cm * 10 m);

WriteLine($"Area of 1 cm * 10 m = $x1 m");

well I made QuantitySystem library especially for units in C#, however its not compile time checking

but I've tried to make it run as I wanted

also it supports expansion so you can define your unique units

http://QuantitySystem.CodePlex.com

also it can differentiate between Torque and Work :) [This was important for me]

the library approach is from Dimension to units all I've seen till now units only approach.

I'm pretty sure Ada has it.

I'm sure you'd be able to do this with most dynamic languages (javascript, python, ruby) by carefully monkey-patching some of the base-classes. You might get into problems though when working with imperial measurements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top