Note that there are some explanatory texts on larger screens.

plurals
  1. POReusing units of measure across different types
    primarykey
    data
    text
    <p><em>(<a href="https://stackoverflow.com/questions/460766/f-units-of-measure-problems-with-genericity">Yes</a>, <a href="https://stackoverflow.com/questions/490780/f-units-of-measure-problems-with-genericity-2">still</a> <a href="https://stackoverflow.com/questions/412459/how-to-generically-remove-f-units-of-measure">playing</a> <a href="https://stackoverflow.com/questions/419521/f-units-of-measure-lifting-values-to-floatsomething">with</a> <a href="https://stackoverflow.com/questions/348853/units-of-measure-in-c-almost">units</a> of <a href="https://stackoverflow.com/questions/1398611/f-ununit-reunit-inside-a-function">measure</a>)</em></p> <p>I've just started using units of measure for something other than float for the first time.</p> <p>After starting off complicated, I discovered that you can re-use units of measure across multiple 'types'.</p> <pre><code>[&lt;Measure&gt;] type mm let a = 1&lt;mm&gt; let b = 1.&lt;mm&gt; </code></pre> <p>Now I'm sitting here wondering how to update my existing mm type to reflect this possibility:</p> <pre><code>[&lt;Measure&gt;] type mm = static member perMetre = 1000.0&lt;mm/m&gt; static member fromMetre (v:float&lt;m&gt;) = v * mm.perMetre static member toMetre (v:float&lt;mm&gt;) = v / mm.perMetre static member lift (v:float) = v * 1.0&lt;mm&gt; </code></pre> <p><strong>The question:</strong> How do I write a function that can convert from <code>mm</code> to <code>m</code> (say) that can handle <code>int&lt;mm> -> int&lt;m></code> AND <code>float&lt;mm> -> float&lt;m></code>?</p> <p><strong>UPDATE</strong></p> <p>Thanks to Kurt, here's the new version:</p> <pre><code>[&lt;Measure&gt;] type mm = static member private perMetreFloat = 1000.0&lt;mm/m&gt; static member private perMetreInt = 1000&lt;mm/m&gt; [&lt;OverloadIDAttribute("float")&gt;] static member fromMetre (v:float&lt;m&gt;) = v * mm.perMetreFloat [&lt;OverloadIDAttribute("float")&gt;] static member toMetre (v:float&lt;mm&gt;) = v / mm.perMetreFloat [&lt;OverloadIDAttribute("float")&gt;] static member lift (v:float) = v * 1.0&lt;mm&gt; [&lt;OverloadIDAttribute("int")&gt;] static member fromMetre (v:int&lt;m&gt;) = v * mm.perMetreInt [&lt;OverloadIDAttribute("int")&gt;] static member toMetre (v:int&lt;mm&gt;) = v / mm.perMetreInt [&lt;OverloadIDAttribute("int")&gt;] static member lift (v:int) = v * 1&lt;mm&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload