title: Strict generic enum conversion in F#
body: <p>I want to write a function with signature <code>int -&gt; 'TEnum</code> that will throw an exception in case the target enum don't contain an input value. Here's my first try:</p>
  
  <pre><code>let parseEnum&lt;'TEnum when 'TEnum : enum&lt;int&gt;&gt; (value : int) : 'TEnum =
      let enumType = typeof&lt;'TEnum&gt;
      if not &lt;| Enum.IsDefined (enumType, value) then
          raise &lt;| ArgumentException (sprintf "Invalid value of %A: %d" enumType value)
  
      enum value
  </code></pre>
  
  <p>The compiler shows me the following error message: "error FS0001: The declared type parameter 'TEnum' cannot be used here since the type parameter cannot be resolved at compile time" (I think that's because of the <code>enum</code> function that have an additional constraint).</p>
  
  <p>Well, okay then, I understand the problem. I'll use <a href="https://msdn.microsoft.com/en-us/library/dd548046.aspx" rel="nofollow">statically resolved type parameter</a>. Here's my second try:</p>
  
  <pre><code>let inline parseEnum2 (value : int) : ^TEnum =
      let enumType = typeof&lt;^TEnum&gt;
      if not &lt;| Enum.IsDefined (enumType, value) then
          raise &lt;| ArgumentException (sprintf "Invalid value of %A: %d" enumType value)
  
      enum value
  </code></pre>
  
  <p>But compiler still complains: "error FS3156: Unexpected token '>' or incomplete expression" at the line <code>typeof&lt;^TEnum&gt;</code>.</p>
  
  <p>What am I doing wrong? How could I write this function?</p>