Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Firstly, here is a trivial example that shows how the syntax should look like:</p> <pre><code>let inline zero&lt; ^NUM when ^NUM : (static member get_Zero: unit-&gt; ^NUM)&gt; (n:^NUM) = (^NUM : (static member get_Zero : unit -&gt; ^NUM) ()) </code></pre> <p>In some cases, you don't need to write the constraints explicitly (the F# compiler will actually warn you about that if you write the above), because some static members are well-known to the compiler and there are standard functions for using them. So, you can use the function and the compiler will infer the constraint:</p> <pre><code>let inline zero (n:^T) = LanguagePrimitives.GenericZero&lt; ^T &gt; </code></pre> <p>Unfortunately, this really doesn't help you, because recursive functions cannot be declared as <code>inline</code> (for obvious reasons - the compiler cannot inline the function at compile time, because it doesn't know how many times), so static constraints are probably not powerful enough for your problem.</p> <p>[<strong>EDIT</strong>: This is actually possible for some functions (see kvb's answer)]</p> <p>I think you'll need <code>NumericAssociations</code> instead, which were alreaday discussed <a href="https://stackoverflow.com/questions/2225949/does-f-have-generic-arithmetic-support">in this question</a> (these are processed at runtime, so they are slower - but are used to implement for example F# matrix type - the matrix can cache the dynamically obtained information, so it is reasonably efficient).</p>
 

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