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

  • Home
  • SEARCH
  • 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 6829815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:32:41+00:00 2026-05-26T22:32:41+00:00

let f (O: obj) = match O with | :? (obj -> list<obj>) ->

  • 0
let f (O: obj) = 
    match O with
        | :? (obj -> list<obj>) -> "win"
        | :? list<obj> -> "list!"
        | _ -> "fail"

Console.WriteLine(f(fun x -> ["lol"]))
Console.WriteLine(f(["lol"]))

prints “fail” twice, as I suppose it should, because I am giving i a function obj -> list<String>, which is not a obj -> list<obj>. Is there any way to make them match though? I could upcast each list into a list<obj> before making an anonymous function out of it, or I could upcast everything to obj before putting it into the list.

Either of those works and makes it match, but i thought this was the problem that covariance/contravariance was meant to have already solved? Correct me if i’m mistaken

  • 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-26T22:32:41+00:00Added an answer on May 26, 2026 at 10:32 pm

    Unfortunately, you can’t solve this using any built-in pattern matching.

    The only way to find out whether an obj value is some F# function is to use F# Reflection and call the FSharpType.IsFunction method on the type. You can check for the case in your example like this:

    open System    
    open Microsoft.FSharp.Reflection    
    
    let f (o : obj) =  
      let ty = o.GetType() 
      if FSharpType.IsFunction(ty) then
        let tyFrom, tyTo = FSharpType.GetFunctionElements(ty)
        if tyTo.IsGenericType && tyTo.GetGenericTypeDefinition() = typedefof<list<_>> then
          printfn "win"
        else 
          printfn "wrong function"
      else
        printfn "not a function"
    
    Console.WriteLine(f(fun x -> "lol"))    // wrong function
    Console.WriteLine(f(fun x -> ["lol"]))  // win
    Console.WriteLine(f(["lol"]))           // not a function
    

    You could encapsulate the behavior in an F# active pattern to make the syntax a bit nicer (and use pattern matching on types). However, another problem is that this doesn’t give you a function that you could use to actually invoke the function dynamically. I don’t think there is a built-in library function for this, so you’ll probably need to use .NET reflection to call the Invoke method dynamically.

    EDIT: There has been similar related questions on SO. The general problem is that you’re matching against some (any) instantiation of a specific generic type, so the same issue arises with lists etc. See for example:

    • F# and pattern matching on generics…
    • Pattern matching against generic type…
    • How to cast an object to a list…
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let compareOn f x (yobj: obj) = match yobj with | :? 'T as
Let's say I have code: obj = MyUser.objects.get(pk=1) name = obj.name age = obj.age
Let me put my code first string strSigned = Request.Params[signed_request]; // JSONObject obj =
Let's consider the following code in Java. package obj; final class First { public
I tried the following: let inline (|OpAdd|_|) (aty:Type, x:obj, y:obj) = if aty.Equals(typeof<'a>) then
When writing C++, let's assume the following line of code: Object* obj = new
Let suppose i have 5 objects and each object contains double values. I want
Let's assume I have some MyObject obj object and want to access a property
Should be an easy one. Let's say I have the following code: void Method()
Let's create an object which inherits from another anonymous object: var obj = Object.create({

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.