Measure ExampleUse the Measure attribute to annotate numeric types with measurements.
F#
Measure. Consider in F# a square may have a width (in feet), and a height (in feet). Its area is equal to its width times its height. Its area must be feet squared.
With units of measurement, we can force the F# compiler to validate that our area is feet squared. We define measurements with the Measure attribute.
First example. Let us demonstrate Measure in a simple program. We introduce two units, feet and area. Feet is a simple int unit. And area is feet squared (feet times feet).
Then We specify width and height variables. These are 10 and 5 feet in size. We use the feet measurement.
Finally We create an areaOfBox variable. This has type of int with measurement area. It is equal to a feet unit times another.
So AreaOfBox here can only be created by multiplying two "feet" measurements together. They cannot be added or computed in another way.
// Use feet as a unit of measure. [<Measure>] type feet // Area is feet squared. [<Measure>] type area = feet ^ 2 // Create a width and a height in feet. let width = 10<feet> let height = 5<feet> // Get the area by multiplying the width and the height. // ... This is an area measure. let (areaOfBox : int<area>) = width * height // Write value of areaOfBox. printfn "%A" areaOfBox
50
Compile-time error. Measurements allow the F# compiler to check programs for valid units. Here we try to get the area of the box by using division.
However Area is not the result of a division. It is the result of a multiplication (a squaring of feet).
So The program does not compile. Another unit, other than area, would need to exist and be used for the division.
[<Measure>] type feet [<Measure>] type area = feet ^ 2 let width = 10<feet> let height = 5<feet> // This does not compile because an area is not based on a division. // ... It must be a multiplication of feet, not a division of feet. let (areaOfBox : int<area>) = width / height
error FS0001: The type 'int<area>' does not match the type 'int'
Some notes. Units of measurement are an advanced feature in F#. For quick programs they are not useful. But for important programs where compile-time checking is valuable, they can help.
Tip Measures are a way to introduce more compile-time validation into a program. They can help us "prove" a program is correct.
Detail Measures are a form of constraint-checking. For critical programs where an error is catastrophic, they are more valuable.
A review. In numeric problems we want to ensure the right numbers are used. Measures can help us do this. They can ensure our variables are applied in a consistent, sane way.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.