3 ms·
Someone on the team needs to know about it. That person needs to set up the API to expect properly typed inputs. Others can remain ignorant about units of measu
by nequo 3y ago
Someone on the team needs to know about it. That person needs to set up the API to expect properly typed inputs. Others can remain ignorant about units of measurement and the compiler will educate them.
F# has this built in but it can be achieved with libraries in some other languages. For example, in Haskell with the dimensional package[1]:
import Numeric.Units.Dimensional.Prelude
import Numeric.Units.Dimensional.NonSI (mile)
speedLimit :: Velocity Double
speedLimit = 30 *~ (mile / hour)
main :: IO ()
main = do
let speed1 = 35 *~ (kilo meter / hour)
let speed2 = 35 *~ (mile / hour)
let speed3 = 35
print (speed1 <= speedLimit)
print (speed2 <= speedLimit)
print (speed3 <= speedLimit)
GHC will give you an error because `speed3` is mistyped.
[1] https://hackage.haskell.org/package/dimensional https://hackage.haskell.org/package/dimensional
- midoridensha 3y agoYou're not getting it. The Mars probe didn't crash because of bad unit conversion. It crashed because there were no units in the data!!! Go read about it yourself. NASA got some data from a contractor. The data had no units at all. The contractor assumed the data to be in some kind of US-standard units. NASA got these raw numbers and assumed they were in some kind of SI units. Having enforced units in your language isn't going to help in this situation at all. They weren't exchanging code, they were exchanging raw data, or more specifically, a table with a bunch of numbers on it. The people at NASA simply assumed some units (instead of calling the contractor and asking), and they would have done this in your scenario too.