{"id":1132,"date":"2014-01-18T23:44:51","date_gmt":"2014-01-18T23:44:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1132"},"modified":"2014-01-19T22:17:09","modified_gmt":"2014-01-19T22:17:09","slug":"units-of-measure","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/units-of-measure\/","title":{"rendered":"Units of Measure"},"content":{"rendered":"<p>F# has the concept of units of measure. In essence we can declare a unit of measure on a numerical type which associates the numeric with a specific &#8220;measurement type&#8221; of data. For example, we might define a value as one of the imperial unit of measure, such as lb. Such as <\/p>\n<p>As you can see, declare a unit of measure we use the [&lt;Measure&gt;] attribute as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;Measure&gt;]\r\ntype lb\r\n<\/pre>\n<p>We can now use this unit on a type as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet weightInPounds = 100&lt;lb&gt;\r\n<\/pre>\n<p>The inferred type here is <em>val weightInPounds : int<lb> = 100<\/em>, so as you can see the unit of measure is tied to the type (in this case an int).<\/p>\n<p>If we wish to add or subtract to\/from the weightInPounds we might be tempted to write<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet newWeight = weightInPounds - 2\r\n<\/pre>\n<p>But we&#8217;ll get the following error <em>The type &#8216;int&#8217; does not match the type &#8216;int<lb>&#8216;<\/em>. This is because the 2 is simply an int and we&#8217;d need convert to the same unit of measurements, as per<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet newWeight = weightInPounds - 2&lt;lb&gt;\r\n<\/pre>\n<p>However using multiplication or division will work without the need for the unit of measure added, so the following is perfectly fine<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet newWeight = weightInPounds * 2\r\n<\/pre>\n<p><strong>Deriving units of measurement from existing units of measurement<\/strong><\/p>\n<p>We can also derived new units of measurement from existing ones. So assuming we have the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;Measure&gt;]\r\ntype lb\r\n\r\n&#x5B;&lt;Measure&gt;]\r\ntype inch\r\n<\/pre>\n<p>We might create a psi (pounds per square inch) as follows<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;Measure&gt;]\r\ntype psi = lb inch^2\r\n<\/pre>\n<p>We could also apply multiple units of measurement as part of the let binding, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet p = 12&lt;lb inch^2&gt;\r\n<\/pre>\n<p><strong>Removing a unit of measure<\/strong><\/p>\n<p>In some cases we might want to remove the unit of measure from a value, for example maybe we have a method that takes an int. We will get a compile time error stating &#8220;The expression was expected to have type in but here has type int<kg>&#8220;. We can simply cast<\/p>\n<p>The code below is trying to set the value i (which we&#8217;ve setup to be an int) with a unit of measure value (where weight is shown as an int<kg>).<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet weight = 12&lt;kg&gt;\r\n\r\nlet i : int = int weight\r\n<\/pre>\n<p><strong>Writing to the console<\/strong><\/p>\n<p>To output a value with a unit of measure, using printn, we don&#8217;t use the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet weight = 12&lt;kg&gt;\r\n\r\nprintfn &quot;%A&quot; weight\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>F# has the concept of units of measure. In essence we can declare a unit of measure on a numerical type which associates the numeric with a specific &#8220;measurement type&#8221; of data. For example, we might define a value as one of the imperial unit of measure, such as lb. Such as As you can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1132","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1132"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1132\/revisions"}],"predecessor-version":[{"id":1141,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1132\/revisions\/1141"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}