TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to F#

F# Measure Example: Units of Measurement

Use the Measure attribute to annotate numeric types with measurements.
Measure. A square has a width (in feet). It has 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.

F# program that uses measure // 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 Output 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.

F# program that causes compile-time measure error [<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 Output 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.

Constraints: Measures are a form of constraint-checking. For critical programs where an error is catastrophic, they are more valuable.

Quote: F# supports static checking of units of measure. Units of measure, or measures for short, are like types in that they can appear as parameters to other types and values... and are checked for consistency by the type-checker (F# Language Specification).

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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf