支持/集成语言测量单位的策略是什么?
我想从纯粹的语言设计角度来看,SI 单位的“实现”需要哪些“功能”(语义上和句法上)。
如果有人声称一种语言对测量单位有很大的支持,那么通常会期望哪种“功能”?
- 只是像特殊文字或语法糖之类的东西?
- 使单元类型安全的特殊约定(但无需昂贵的运行时包装)?
- 用于分数计算的特殊数学模式?
- 单位之间的自动转换和强制?
例如,F# 集成了对该语言中测量单位的支持。它与 Java 的一个库相比有何改进?
应将哪些功能内置到语言中以提高单元的可用性?哪些功能不一定与测量单位相关,但可以使实现更好?
I wonder from purely language-design point of view which "features" (semantically and syntactically) an "implementation" of SI units would require.
Which "functionality" is generally expected if someone claims that a language has great support for units of measurements?
- Just something like special literals or syntactic sugar?
- Special conventions which make units typesafe (but without costly runtime wrapping)?
- A special math mode for computations with fractions?
- Automatic conversions and coercion between units?
For instance F# has integrated support for units of measurements in the language. How does it improve over e. g. a library for Java?
Which features should be built into the languages to improve usability of units? Which features are not necessarily related to units of measurement but make an implementation nicer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
F# 相对于 Java UOM 库的优势很简单——类型安全。如果您尝试添加
3.
和4.
,您将收到编译时错误。在 F# 中,UOM 在类型检查后被删除,因为它是唯一具有此类功能的 .NET 语言。
回复评论,这里是一个简单的分数类型及其用法:
返回的值是
Fraction
类型,其AsFloat
值为0.5333333333
。F#'s advantage over a Java UOM library is simple - type safety. You will get compile-time errors if you attempt to add
3.<s>
and4.<m / s>
.In F#, UOM are erased after the type-checking, because it is the only .NET language with such feature.
In reply to comment, here is a simple fraction type and its usage:
And the value returned is of type
Fraction<kg m/s>
and itsAsFloat
value is0.5333333333
.从语言设计的角度来看,能够:
这一切都可以完成 并完成更多的10 年前 使用 C++ 模板元编程。
Haskell 还有一个实现,它使用 Haskell 的(有限)能力对类型类实例执行编译时计算。但它并不完整(据我所知,没有分数幂,也没有非 SI 单位)。
From a language-design point of view, it's enough to be able to :
This all can be done and was done more than 10 years ago with C++ template metaprogramming.
There's also an implementation for Haskell that uses Haskell's (limited) ability to perform compile-time calculations over typeclass instances. It's not as complete though (AFAIK no fractional powers and no non-SI units).
迷人。我在 google 上搜索了这个叮当声,“我确信我可以在 Smalltalk 中轻松地做到这一点”,并偶然发现了 Frink http://futureboy .us/frinkdocs/,它针对 JVM 和 Android。
当然,关键是转化
和不确定性
我确信您可以使用 Java 中的类来完成此操作,当然也可以使用 Ada 来完成此操作,但这会需要大量(抱歉)输入。
Fascinating. I googled this tinkling,"I am sure I could do this quite easily in Smalltalk," and stumbled upon Frink http://futureboy.us/frinkdocs/ which targets the JVM and Android.
and of course key to this is conversion
and uncertainty
I'm sure you could do this with classes in Java and definitely with Ada, but that would be a lot of (I'm sorry) typing.
20 世纪 80 年代,ACM SIGPLAN 公告中有一篇关于此的好论文。这个想法是类型安全加上你在高中物理中学到的单元分析:例如,速度是英里/小时,时间是小时,因此将它们相乘得出以英里为单位的距离,并且类型系统强制执行该距离这样你就不会犯类别错误。语言或元语言将有办法指定所有这些。另一个例子,我最近在Java中做了一些货币工作,其中CurrencyValue除以CurrencyValue得到一个BigDecimal,即汇率;一个CurrencyValue乘以或除以一个BigDecimal得到另一个CurrencyValue,等等。
这基本上就是你过去做旧的多项选择PSSC物理考试的方式:忘记物理,只记住宇宙的一些常数,然后推理问题给定指定数量的单位和所需答案的单位,那么您可以自己构造所需的公式。
There was a good paper in the ACM SIGPLAN Notices about this in the 1980s. The idea was type safety plus the kind of unit analysis you learn in high school physics: for example, a speed is miles/hour, a time is hours, so multiplying them together gives a distance in miles, and the type system enforces that for you so you can't make category mistakes. The language or meta-language would have ways to specify all that. As another example, I recently did some currency work in Java where a CurrencyValue divided by a CurrencyValue gave a BigDecimal, i.e. an exchange rate; a CurrencyValue multipled or divided by a BigDecimal gave another CurrencyValue, etc.
This is basically how you used to do the old multi-choice PSSC Physics exams: forget the physics, just memorize a few constants of the universe, then just reason about the problem given the units of the specified quantities and the units of the answer required, then you can construct the formula required yourself.
Java 库 (unitsofmeasurement.org) 的后续版本现在可以在 unitsofmeasurement.github.io 下找到。它是 Java 测量单位标准 JSR 363 的所在地。一探究竟。
The successor to the library for Java (unitsofmeasurement.org) can now be found under unitsofmeasurement.github.io. It is home to the Java standard for Units of Measurement, JSR 363. Check it out.