Less Defects with F# Units of Measure

One of the most powerful things that F# has is its ability to create custom units of measure. F# allows you to set an explicit type to a unit. That’s right…you will get compile time checking on any of the units that you use within your application.

If your application does use any type of units, then determining what type of unit the input or output is expecting can be tricky. Even NASA has had this problem before. With the compile time checking of any discrepancies for the units, then these types of bugs will disappear all together.

For the most used units Microsoft already has them built in and can be directly used.

open Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols

let meter = 1<m>

But for custom units they can be easily defined using the Measure attribute.

[<Measure>] type pixel
[<Measure>] type inch

Then we can use them like normal values except, as you can see, the pixels value is strongly typed as an int of pixel.

let pixels = 200<pixel>

val pixels : int<pixel> = 200

Both units can be used in creating values for calculations…

let pixelsPerInch = 300<pixel/inch>

Or even as a totally new unit.

[<Measure>] type ppi = pixel/inch

Within our calculation, if we specify what unit to multiply with we’ll get a correct result type. In this example we end up with 600 pixels.

let ppi = pixelsPerInch * 2<inch>

val ppi : int<pixel> = 600

Omit the inch type in the calculation and you get a different type – pixel/inch.

let ppi = pixelsPerInch * 2

val ppi : int<pixel/inch> = 600

Now let’s say we have a function to do some other calculation.

let pixelsByGivenInches inches: float<pixel/inch> =
    inches + 1.0

But this will result in a compilation error.

Instead we need to specify what unit we want to do the calculation in.

let pixelsByGivenInches inches: float<pixel/inch> =
    inches + 1.0<pixel/inch>

If we run this in the interactive our result will actually be the type float of pixel/inch.

val pixelsByGivenInches : inches:float<pixel/inch> -> float<pixel/inch>

And now we can use the above function and pass in the correct type in its parameter.

pixelsByGivenInches 2.0<pixel/inch>

val it : float<pixel/inch> = 3.0

Hopefully this helped to understand another really useful feature that F# has to offer, especially in the world of real world applications that can be used for the enterprise as well as for science.

Of course, much more information can be found at F# For Fun and Profit and MSDN.

Jonathan Wood

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

2 days ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

1 week ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

3 weeks ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

1 month ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

1 month ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago