位置泛型类型的错误,我创建了一个基于Enumerable.OfType<'T>()
的序列的管道能够ofType<'T>
功能:与视功能
let ofType<'T> (sequence : _ seq) = sequence.OfType<'T>()
同.fsx
文件中使用也能正常工作;它仍然没有当我把它变成一个模块:
module Seq =
let ofType<'T> (sequence : _ seq) = sequence.OfType<'T>()
它停止工作,当我移动到另一个脚本文件,(要能够从其他地方访问它)在另一个顶层模块把它包:
module Prelude =
open System.Linq
module Seq =
let ofType<'T> (sequence : _ seq) = sequence.OfType<'T>()
我引用这个从我原来的脚本文件,打开Prelude
模块,并调用这样的功能:
let getXmlIncludes (xtype : Type) =
xtype.GetCustomAttributes() |> Seq.ofType<XmlIncludeAttribute>
这导致Seq.ofType<XmlIncludeAttribute>
是坊间d为错误,用消息
error FS0001: Type mismatch. Expecting a
Collections.Generic.IEnumerable<Attribute> -> 'a
> but given a
Collections.Generic.IEnumerable<Attribute> -> Collections.Generic.IEnumerable<XmlIncludeAttribute>
The type 'obj' does not match the type 'Attribute'
误差保持不变时,我直接移动ofType<'T>
到Prelude
模块。
为什么会发生这种情况,我该如何让它不发生?
(我试图改变_ seq
类型为sequence
参数'TSeq seq
,导致广受欢迎的
warning FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'TSeq has been constrained to be type 'obj'.
,但并不会改变对错误的东西)
我似乎无法重现你的错误。也许你可以提供一个显示错误的回购。 – Ringil
现在的'Seq.cast'已经变成了一个'Enumerable.OfType'了吗? – Sehnsucht
@Sehnsucht'OfType'与'.Where(x => x是typeof(T))'相同。按类型过滤。按照我的答案中的第一个链接,您将在同一屏幕上看到“OfType”和“Cast”实现。 –