简体   繁体   English

F#,Deedle:限制为 Frame<obj,obj> 类型的通用框架

[英]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:我有一个接收 obj 并尝试猜测它是字符串、Deedle Frame 还是其他东西的函数:

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 >.问题是 Frame<'a,'b> 被限制为类型 Frame<obj,obj>。 So if I had someFrame of type Frame< int,string >, exampleF would output “something else”.因此,如果我有 Frame< int,string > 类型的 someFrame,exampleF 将输出“别的东西”。 However, if exampleF had another branch with “ :?但是,如果 exampleF 有另一个带有“ :? Frame< int,string > as d ->”, someFrame would be correctly caught. Frame< int,string > as d ->”, someFrame 将被正确捕获。

How can I capture all Frames in a pattern matching like that without having to specify the inner types?如何在无需指定内部类型的情况下以这样的模式匹配捕获所有帧?

Jim Foye helped me to find the answer: Jim Foye 帮我找到了答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM