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.
A common problem
Have you ever 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.
If you’ve ever encountered this, you’ll know that it’s very difficult to guarantee that the correct values are being passed into a method. After all, a
decimal property entitled
Length doesn’t exactly give anything away.
Introducing Units of Measure
If you’re still reading and didn’t get bored hearing about outdated units of measurement still employed on the railways, it’s time to get to the point of this article: F# Units of Measure.
F# has a very rich type system, and is often hailed as a fantastic language for domain modelling. One of the features it boasts is the ability to create custom types that represent a unit of measurement:
It’s okay if you don’t understand entirely what’s going on here, as long as you understand how units of measure add that additional level of safety to your code.
Solving the railway problem
So if you’ve been paying attention, you’ll remember that I mentioned the downright archaic units of measurement still being used on the British railway network. Now we’ve seen how useful units of measure can be on something as commonplace as time measurements, lets see how things go with mapping some of these obscure measurements.
1 Chain is equal to 100 Links, 4 Rods, or 20.1168 metres. A Furlong is consisted of 10 Chains, and a Statute Mile is consisted of 80 Chains. This sounds ridiculously complicated, but this is easily mapped out using F#
How easy was that? You are able to codify this just like any other domain code, and what’s even better is that it will make sure you’re passing values of the correct unit in, and outputting the correct unit.
As more and more classic F# features make it into newer versions of C#, such as record types and pattern matching, I’m hopeful that units of measurement are added in so that they can get the mainstream usage that they deserve.
Of all the features in F#, I feel that units of measurement is something that would genuinely make building software much better were it in more languages. I’ve only scratched the surface of what you can do with them, but you can find out a lot more about them from the Microsoft Docs, and this wouldn’t be an F# blog post if I didn’t reference F# for Fun and Profit.