Search code examples
.netfunctional-programmingf#deedle

F#, Deedle: Generic Frame constrained to type Frame< obj,obj >


I have a function that receives an obj and tries to guess if it’s a string, a Deedle Frame or something else:

let exampleF (data : obj) =
    match data with
    | :? string as s -> "string: " + s
    | :? Frame<'a,'b> as d -> "Frame"
    | _ -> "something else"

The problem is that Frame<’a,’b> is constrained to type Frame< obj,obj >. So if I had someFrame of type Frame< int,string >, exampleF would output “something else”. However, if exampleF had another branch with “ :? Frame< int,string > as d ->”, someFrame would be correctly caught.

How can I capture all Frames in a pattern matching like that without having to specify the inner types?


Solution

  • Jim Foye helped me to find the answer:

    let exampleF data =
        match data.GetType() with
        | typ when typ.IsGenericType && typ.GetGenericTypeDefinition() = typedefof<Frame<_,_>> -> "Frame"
        | typ when typ = typeof<string> -> "string"
        | _ -> "something else"