F# Units of Measure. Utilising F# to make your code safer. | by Carlton Upperdine | May, 2021 | Medium

submited by
Style Pass
2021-05-23 16:30:08

F# is a statically-typed, functional-first programming language that runs on the .NET platform. Even though I’m usually writing C# in my day job (and quite a bit of TypeScript), I am massive fan of F# and functional programming in general. I won’t spend any more time introducing F#, as that’s a different post for a different day, but I will say that a good amount of the newer features of C# have been stolen straight from F#. One feature that has yet to be “appropriated” by C# is Units of Measure.

Have you e ver written code with a specific unit in mind, but no way to actually enforce it? The typical solution to this is to make sure that you are naming your variables, functions and parameters in a descriptive way, but this is more of a prevention than a cure.

For a real world example: I have written code where the application domain is the British railway network. What most people don’t know about the British railway system is that it still uses obscure units of measurement such as Chains, Links, and Rods. As you can imagine, not everybody new to the domain will be aware of this, and naively assume that a train length would be measured in Metres.

Leave a Comment