使用'flexible'类型参数针对通用类型进行模式匹配 [英] Pattern matching against generic type using 'flexible' type parameter
本文介绍了使用'flexible'类型参数针对通用类型进行模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
match value with
| :? list<#SomeType> as l -> l //Is it possible to match any list of a type derived from SomeType?
| _ -> failwith "doesn't match"
推荐答案
不,不幸的是,不可能做这样的事情-CLR没有提供进行这种类型测试的任何有效方法.参见如何投射对象到F#和
No, it's unfortunately not possible to do something like this - the CLR doesn't provide any efficient way of doing that kind of type test. See How to cast an object to a list of generic type in F# and F# and pattern matching on generics in a non-generic method implementing an interface for a few (rather ugly) solutions.
这篇关于使用'flexible'类型参数针对通用类型进行模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文