title: How to downcast from obj to option<obj>?
body: <p>I have a function that takes a parameter of type object and needs to downcast it to an <code>option&lt;obj&gt;</code>.</p>
  
  <pre><code>member s.Bind(x : obj, rest) =
      let x = x :?&gt; Option&lt;obj&gt;
  </code></pre>
  
  <p>If I pass (for example) an <code>Option&lt;string&gt;</code> as <code>x</code>, the last line throws the exception: <em>Unable to cast object of type 'Microsoft.FSharp.Core.FSharpOption'1[System.String]' to type 'Microsoft.FSharp.Core.FSharpOption'1[System.Object]'.</em></p>
  
  <p>Or, if I try a type test:</p>
  
  <pre><code>member s.Bind(x : obj, rest) =
     match x with
      | :? option&lt;obj&gt; as x1 -&gt; ... // Do stuff with x1
      | _ -&gt; failwith "Invalid type"
  </code></pre>
  
  <p>then <code>x</code> never matches <code>option&lt;obj&gt;</code>.</p>
  
  <p>In order to make this work, I currently have to specify the type the option contains (e.g. if the function is passed an <code>option&lt;string&gt;</code>, and I downcast the parameter to that rather than <code>option&lt;obj&gt;</code>, the function works.</p>
  
  <p>Is there a way I can downcast the parameter to <code>option&lt;obj&gt;</code> without specifying what type the option contains? I've tried <code>option&lt;_&gt;</code>, <code>option&lt;#obj&gt;</code>, and <code>option&lt;'a&gt;</code> with the same results.</p>
  
  <p>By way of background, the parameter needs to be of type <code>obj</code> because I'm writing an interface for a monad, so Bind needs to bind values of different types depending on the monad that implements the interface. This particular monad is a continuation monad, so it just wants to make sure the parameter is <code>Some(x)</code> and not <code>None</code>, then pass <code>x</code> on to rest. (The reason I need the interface is because I'm writing a monad transformer and I need a way to tell it that its parameter monads implement bind and return.)</p>
  
  <p><strong>Update:</strong> I managed to get around this by upcasting the contents of the option before it becomes a parameter to this function, but I'm still curious to know if I can type-test or cast an object (or generic parameter) to an option without worrying about what type the option contains (assuming of course the cast is valid, i.e. the object really is an option).</p>