4 ms·
I have built my own implementations of units in a couple of functional languages (Haskell and Ocaml) using shadow types. How this pattern works is you tag a num
by seanhunter 2y ago
I have built my own implementations of units in a couple of functional languages (Haskell and Ocaml) using shadow types. How this pattern works is you tag a numeric type with an extra algebraic type defining the units and then define how the arithmetic operators handle the units. The cool thing about doing it this way is the shadow types compile away so there is zero runtime cost but you can assert and pattern match on the types and put them in your unit tests etc and your code literally won't compile if you mess the units up in some way.