Re:Can 1/3 and 1/3 = 2/6? It seemed so
by bradlys on Tuesday May 26, 2020 @11:05AM
(#23312054)
Interesting - I never thought of CS as being something to help with this. Instead, Iād think of other engineering fields where there is a heavy reliance on correct units and unit conversion. Pound of feathers vs pound of gold , etc...
[ Parent
| on HN ]
F# offers a nifty "unit of measure" language feature to help with that. You might have
[<Measure>] type lbFeather
[<Measure>] type lbGold
[<Measure>] type lbLead
let convertGoldToLead ( weight : float<lbGold> ) = 1.0<lbLead> / 1.0<lbGold> * weight
// works
let barOfGold = 1.0<lbGold>
let barOfLead = convertGoldToLead barOfGold
// doesn't work
let bagOfFeathers = 1.0<lbFeather>
let leadDuck = convertGoldToLead bagOfFeathers
// Error reported: mismatched units.