I created a pipe-able ofType<'T>
function for sequences based on Enumerable.OfType<'T>()
:
let ofType<'T> (sequence : _ seq) = sequence.OfType<'T>()
Using this within the same .fsx
file works fine; it still does when I put it into a module:
module Seq =
let ofType<'T> (sequence : _ seq) = sequence.OfType<'T>()
It stops working when I move it into another script file and (to be able to access it from elsewhere) wrap it in another top-level module:
module Prelude =
open System.Linq
module Seq =
let ofType<'T> (sequence : _ seq) = sequence.OfType<'T>()
I reference this from my original script file, open the Prelude
module and call the function like this:
let getXmlIncludes (xtype : Type) =
xtype.GetCustomAttributes() |> Seq.ofType<XmlIncludeAttribute>
That causes Seq.ofType<XmlIncludeAttribute>
to be marked as an error, with the message
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'
The error remains the same when I move ofType<'T>
directly into the Prelude
module.
Why does this happen, and how can I make it not happen?
(I tried changing the _ seq
type for the sequence
parameter to 'TSeq seq
, which results in the ever-popular
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'.
but doesn't change anything about the error.)