实际上有两种方法可以指定要获取的类型。Brian 发布了如何通过显式指定函数的类型参数来做到这一点:
let res = Seq.cast<Match> v.Captures
另一种选择是使用可以放置在任何 F# 表达式周围的类型注释并指定表达式的类型 - 这样您可以提示编译器类型推断(通过说某些表达式具有特定类型)。如果您以某种巧妙的方式提供信息,编译器将能够确定类型参数Seq.cast
应该是什么。几个例子:
// By specifying type of the value
let (res:seq<Match>) = Seq.cast v.Captures
// By specifying return type of a function
let getCaptures () : seq<Match> =
// ...
Seq.cast v.Captures
// By specifying element type when iterating over the result
for (m:Match) in Seq.cast v.Captures do
// ...
在所有选项中,我认为 Brians(显式)和我的第二个(函数的返回类型)是最惯用的选项,但是,您可以选择任何您认为最易读的选项。