title: f# discriminated union generic
body: <p>What is the syntax for a generic TryValue type that can return a Value of 'a or an Error of 'b?</p>
  
  <pre><code>type TryValue =
      | Value of 'a
      | Error of 'b
  </code></pre>
  
  <p>If error is a string then it's fine:</p>
  
  <pre><code>type 'a TryValue =
      | Value of 'a
      | Error of string
  </code></pre>
  
  <p>I'd like to define a tryRun function that takes an error creator, a function and a parameter that will try to run the function with the parameter and on any error use the error creator function to create an error:</p>
  
  <pre><code>let tryRun createErrorFn param fn =
      try 
          Value (fn param)
      with
          | ex  -&gt; Error (createErrorFn ex.Message param)
  </code></pre>
  
  <p>And a wrapper for any function to be wrapped by a tryRun:</p>
  
  <pre><code>let wrapTryRun createErrorFn fn param =
      match param with
      | Value a -&gt; tryRun createErrorFn a fn
      | Error e -&gt; Error e
  </code></pre>
  
  <p>Now I can run a list of functions (pseudo code, have not yet fully worked this out):</p>
  
  <pre><code>let createErrorFn errorMessage param =
      URLProcessignError {url=param.url;errorMessage=errorMessage}
  [fn1;fn2] |&gt; List.fold (fun acc fn -&gt; (wrapTryRun createErrorFn fn acc))
  </code></pre>