views:

477

answers:

10

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.

+1  A: 

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.

Pi
I was referring to the system by which you can define arbitrary units of measurement, but that's interesting nonetheless.
Cody Brocious
I don't have my manual for my '48 handy, but I believe it lets you do this as well.
Pi
+3  A: 

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.

Greg Hewgill
A: 

I'm pretty sure Ada has it.

TraumaPony
I don't think Ada has this feature directly, but you can certainly declare types that are based on another type (ie. integer or float) but are incompatible types as far as the compiler is concerned. So you couldn't directly add metres and feet without converting one of them.
Greg Hewgill
There is a library for it: "Units of measurement for Ada", http://www.dmitry-kazakov.de/ada/units.htm
Peter Mortensen
+6  A: 

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

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.

Eevee
+1  A: 

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

Jon Harrop
+1  A: 

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/

Lukasz
A: 

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.

digitala
+2  A: 

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

puetzk
A: 

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.