F#: How to downcast from obj to option<obj>?
I have a function that takes a parameter of type object and needs to downcast it to an option&lt;obj&gt;.

...<br>
member s.Bind(x : obj, rest) =<br>
&nbsp;&nbsp;&nbsp;&nbsp;let x = x :?> Option&lt;obj&gt;

If I pass (for example) an Option&lt;string&gt; as x, the last line throws the exception: Unable to cast object of type 'Microsoft.FSharp.Core.FSharpOption'1[System.String]' to type 'Microsoft.FSharp.Core.FSharpOption'1[System.Object]'.

Or, if I try a type test:

...<br>
member s.Bind(x : obj, rest) =<br>
&nbsp;&nbsp;&nbsp;&nbsp;match x with<br>
&nbsp;&nbsp;&nbsp;&nbsp;| :? option&lt;obj&gt; as x1 -> ... // Do stuff with x1<br>
&nbsp;&nbsp;&nbsp;&nbsp;| _ -> failwith "Invalid type"

then x never matches option&lt;obj&gt;.

In order to make this work, I currently have to specify the type the option contains (e.g. if the function is passed an option&lt;string&gt;, and I downcast the parameter to that rather than option&lt;obj&gt;, the function works.

Is there a way I can downcast the parameter to option&lt;obj&gt; without specifying what type the option contains? I've tried option&lt;_&gt;, option&lt;#obj&gt;, and option&lt;'a&gt; with the same results.

By way of background, the parameter needs to be of type obj 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 Some(x) and not None, then pass x 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.)

Thanks all.
F#: How to downcast from obj to option<obj>?
===

I have a function that takes a parameter of type object and needs to downcast it to an option&lt;obj&gt;.

...<br>
member s.Bind(x : obj, rest) =<br>
&nbsp;&nbsp;&nbsp;&nbsp;let x = x :?> Option&lt;obj&gt;

If I pass (for example) an Option&lt;string&gt; as x, the last line throws the exception: Unable to cast object of type 'Microsoft.FSharp.Core.FSharpOption'1[System.String]' to type 'Microsoft.FSharp.Core.FSharpOption'1[System.Object]'.

Or, if I try a type test:

...<br>
member s.Bind(x : obj, rest) =<br>
&nbsp;&nbsp;&nbsp;&nbsp;match x with<br>
&nbsp;&nbsp;&nbsp;&nbsp;| :? option&lt;obj&gt; as x1 -> ... // Do stuff with x1<br>
&nbsp;&nbsp;&nbsp;&nbsp;| _ -> failwith "Invalid type"

then x never matches option&lt;obj&gt;.

In order to make this work, I currently have to specify the type the option contains (e.g. if the function is passed an option&lt;string&gt;, and I downcast the parameter to that rather than option&lt;obj&gt;, the function works.

Is there a way I can downcast the parameter to option&lt;obj&gt; without specifying what type the option contains? I've tried option&lt;_&gt;, option&lt;#obj&gt;, and option&lt;'a&gt; with the same results.

By way of background, the parameter needs to be of type obj 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 Some(x) and not None, then pass x 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.)

Thanks all.