F# Pattern Matching: Matching functions/lists of subtypes?

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> 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.

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> 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...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions