|
|
|
|
|
by fuzztester
1 day ago
|
|
>I like the idea of attaching units to a number, so that if A is 9 volts, B is 3 amps, then A*B yields 27 watts. I asked an LLM: >do fsharp and ada also have the units of measure feature. it said: Yes—both F# and Ada support units of measure, though in different ways.
F# has first-class, built-in language support for units of measure, whereas Ada does not mandate a single built-in units system in the core ISO standard, but GNAT provides compile-time dimension checking via aspects, and third-party Ada libraries implement it as well.
F# (Built-in Language Feature)
F# treats units of measure as a first-class feature of its static type system:
Compile-time safety, zero runtime cost: Units are checked at compile time and erased in compiled .NET code, so there is no runtime overhead (and no unit metadata carried around at runtime).
Algebraic unit inference: You define base units with [<Measure>] (such as type V or type A, or use SI units from FSharp.Data.UnitSystems.SI) and combine them with multiplication, division, and powers. Multiplying volts (float<V>) by amps (float<A>) automatically yields float<V A> (or a derived unit like W if defined).
Type inference: Functions working over units can often infer generic measure parameters (like float<'u>) automatically without heavy boilerplate.
Ada (Compiler Aspects & Libraries)
The core Ada language standard does not define a single universal units-of-measure syntax, so approaches vary depending on your compiler and libraries:
GNAT Dimension_System and Dimension aspects: GNAT (starting from Ada 2012 extensions) supports compile-time dimensional analysis using the Dimension_System and Dimension aspects (including System.Dim.Mks). Like F#, checks happen at compile time with no runtime overhead, though it is a GNAT extension rather than portable across every Ada compiler.
Ada libraries: Developers also use dedicated libraries—such as Dmitry Kazakov's Units of measurement for Ada or SI unit packages—which handle physical dimensions, conversions, and even shifted units like Celsius or Fahrenheit (some doing checks at runtime or via type/record representations. |
|