Are units of measurement unique to F#? [closed]

孤人 提交于 2019-12-12 07:32:18

问题


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:


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.




回答2:


Nemerle had compiler-checked Units of Measure in 2006.

http://nemerle.org

http://nemerle.org/forum.old/viewtopic.php?t=265&view=previous&sid=00f48f33fafd3d49cc6a92350b77d554




回答3:


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




回答4:


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.




回答5:


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.




回答6:


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




回答7:


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/




回答8:


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");



回答9:


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.




回答10:


I'm pretty sure Ada has it.




回答11:


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.



来源:https://stackoverflow.com/questions/107243/are-units-of-measurement-unique-to-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!