我正在尝试在 IEnumerable 上应用 Seq 函数。更具体地说,它是System.Windows.Forms.HtmlElementCollection
它实现了ICollection
and IEnumerable
.
自从 F#seq
是一个 IEnumerable,我想我可以写
let foo = myHtmlElementCollection |> Seq.find (fun i -> i.Name = "foo")
但编译器不会有任何这些,并抱怨说“类型‘HtmlElementCollection’与类型‘seq’不兼容”.
然而,编译器愿意接受 IEnumerablefor .. in ..
序列表达:
let foo = seq { for i in myHtmlElementCollection -> i } |> Seq.find (fun i -> i.Name = "foo")
我需要做什么才能像任何旧的 F# 一样对待 IEnumerableseq
?
NB:这个问题被标记为重复,但事实并非如此。链接的“重复”是关于untyped序列(来自 seq { for ... } 表达式的 seq ),而我的问题是关于typed序列。
F# 的类型seq<'a>
相当于generic IEnumerable<'a> http://msdn.microsoft.com/en-us/library/9eekhta0%28v=vs.110%29.aspx in System.Collections.Generic
,但不至于非通用的 IEnumerable http://msdn.microsoft.com/en-us/library/system.collections.ienumerable%28v=vs.110%29.aspx。您可以使用Seq.cast<'a> http://msdn.microsoft.com/en-us/library/ee370344.aspx从非通用函数转换为通用函数:
let foo = myHtmlElementCollection
|> Seq.cast<HtmlElement>
|> Seq.find (fun i -> i.Name = "foo")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)