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?
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?
Integrating language skills helps language learners to develop their ability in using two or more of the four skills within real context and also in their real life. All the language skills are vital in teaching and learning process and combination of the language skills has positive effects on student success.
Knowing the units of measurement that correspond with a number can give you so much more information than a digit sitting there by itself. Units can: Help to show another person the exact amount you have.
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>
and 4.<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:
type Fraction<[<Measure>] 'a>(a : int, b : int) =
member __.Divisor = a
member __.Dividend = b
member __.AsFloat = float a / float b
static member (*) (a : Fraction<'a>, b : Fraction<'b>) : Fraction<'a * 'b> =
Fraction(a.Divisor * b.Divisor, a.Dividend * b.Dividend)
type [<Measure>] m
type [<Measure>] kg
type [<Measure>] s
let a = Fraction<m / s>(4, 3)
let b = Fraction<kg>(2, 5)
let ``a times b`` = a * b
And the value returned is of type Fraction<kg m/s>
and its AsFloat
value is 0.5333333333
.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With