Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5994341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:48:16+00:00 2026-05-22T23:48:16+00:00

I have a function that takes a parameter of type object and needs to

  • 0

I have a function that takes a parameter of type object and needs to downcast it to an option<obj>.

member s.Bind(x : obj, rest) =
    let x = x :?> Option<obj>

If I pass (for example) an Option<string> 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:

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

then x never matches option<obj>.

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<string>, and I downcast the parameter to that rather than option<obj>, the function works.

Is there a way I can downcast the parameter to option<obj> without specifying what type the option contains? I’ve tried option<_>, option<#obj>, and option<'a> 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.)

Update: 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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:48:17+00:00Added an answer on May 22, 2026 at 11:48 pm

    There isn’t any nice way to solve this problem currently.

    The issue is that you’d need to introduce a new generic type parameter in the pattern matching (when matching against option<'a>), but F# only allows you to define generic type parameters in function declarations. So, your only solution is to use some Reflection tricks. For example, you can define an active pattern that hides this:

    let (|SomeObj|_|) =
      let ty = typedefof<option<_>>
      fun (a:obj) ->
        let aty = a.GetType()
        let v = aty.GetProperty("Value")
        if aty.IsGenericType && aty.GetGenericTypeDefinition() = ty then
          if a = null then None
          else Some(v.GetValue(a, [| |]))
        else None
    

    This will give you None or Some containing obj for any option type:

    let bind (x : obj) rest =   
        match x with    
        | SomeObj(x1) -> rest x1
        | _ -> failwith "Invalid type"
    
    bind(Some 1) (fun n -> 10 * (n :?> int))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF function that takes a complex type as its input parameter.
I have a function that takes, amongst others, a parameter declared as int privateCount
In C++ it's OK to have a funcction that takes a function local type:
I have a powershell function that takes a parameter like this: function whatever {
I have a function which takes a parameter named p_categories , of type smallint[]
I have a function that takes a parameter and produces a result. Unfortunately, it
I have a function that takes 2 parameters : 1 = XML file, 2
I have a calc function in java script that takes three integer parameters, following
I have a function called Colorbox (jQuery plugin) that takes a number of parameters
I have a javascript function (class) that takes a function reference as one paremter.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.