{"id":2833,"date":"2015-02-02T21:57:43","date_gmt":"2015-02-02T21:57:43","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2833"},"modified":"2015-02-02T21:57:43","modified_gmt":"2015-02-02T21:57:43","slug":"more-units-of-measure","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/more-units-of-measure\/","title":{"rendered":"More Units of Measure"},"content":{"rendered":"<p>In a previous post I looked at the <a href=\"http:\/\/putridparrot.com\/blog\/units-of-measure\/\" title=\"Units of Measure\" target=\"_blank\">unit of measure<\/a> feature of F#. Now it&#8217;s time to revisit the topic.<\/p>\n<p>So as a refresher, we can define a unit of measure in the following manner<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;Measure&gt;]\r\ntype kg\r\n<\/pre>\n<p>and we use the unit of measure thus<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet weight = 75&lt;kg&gt;\r\n<\/pre>\n<p>Now it&#8217;s important to note that the unit of measure feature only exists at compile time. At runtime there&#8217;s no concept of the unit of measure, i.e. we cannot use reflection or anything else to see what the unit used was as it no longer exists. However at compile time the code will not build if we try and use a value with a unit of measure on which is not compatible with another unit of measure.<\/p>\n<p>But there&#8217;s more we can do with a unit of measure which makes them even more useful. We can supply a unit of measure with static member functions, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;Measure&gt;\r\ntype g = \r\n   static member toKilograms value : float&lt;g&gt; = value \/ 1000.0&lt;g\/kg&gt;\r\nand &#x5B;&lt;Measure&gt; kg = \r\n   static member toGrams value : float&lt;kg&gt; = value * 1000.0&lt;g\/kg&gt;\r\n<\/pre>\n<p>In the example above we use a mutually recursive type (using the <strong>and<\/strong> syntax). This allows the g type to use the kg type and vice versa.<\/p>\n<p>With this code we can now write<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet kilograms = g.toKilograms 123&lt;g&gt;\r\n<\/pre>\n<p>The result of this will assign the value kilograms with the result of the toKilograms function as well as setting it&#8217;s unit of measure to kg.<\/p>\n<p>Whilst assigning a unit of measure to a numerical value is simple enough, to apply it to an existing value requires the use of an F# core function, from the <em>LanguagePrimitives<\/em> module.<\/p>\n<p>This code creates a value with a unit of measure<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\n&#x5B;&lt;Measure&gt;] \r\ntype g = \r\n    static member create(value : float) = LanguagePrimitives.FloatWithMeasure&lt;g&gt; value\r\n<\/pre>\n<p>So in the above code we might pass in a value and using the <em>FloatWithMeasure<\/em> function we apply the g unit of measure<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet weight = 123\r\nlet weightInGrams = g.create weight\r\n<\/pre>\n<p>The <em>weightInGrams<\/em> value will now have the g unit of measure applied to it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post I looked at the unit of measure feature of F#. Now it&#8217;s time to revisit the topic. So as a refresher, we can define a unit of measure in the following manner &#x5B;&lt;Measure&gt;] type kg and we use the unit of measure thus let weight = 75&lt;kg&gt; Now it&#8217;s important to [&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-2833","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\/2833","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=2833"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2833\/revisions"}],"predecessor-version":[{"id":2840,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2833\/revisions\/2840"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}