F# Seq模块的静态扩展方法

F# Seq模块的静态扩展方法,f#,extension-methods,F#,Extension Methods,根据,F#支持对象实例和静态类的扩展方法。例如: module CollectionExtensions = type System.Linq.Enumerable with static member RangeChar(first:char, last:char) = {first .. last} open ExtensionFSharp.CollectionExtensions 如果我键入System.Linq.Enumerable.,则Intell

根据,F#支持对象实例和静态类的扩展方法。例如:

module CollectionExtensions = 
    type System.Linq.Enumerable with   
        static member RangeChar(first:char, last:char) = {first .. last}

open ExtensionFSharp.CollectionExtensions 
如果我键入
System.Linq.Enumerable.
,则Intellisense窗口中将显示静态方法
RangeChar

我想为Seq模块添加一个静态方法,
for_alli
。我已将上面的以下代码修改如下:

module SeqExtensions =
    type Microsoft.FSharp.Collections.Seq with   (* error on this line *)
        static member for_alli f l =
            l
            |> Seq.mapi (fun i x -> i, x)
            |> Seq.for_all (fun (i, x) -> f i x)
尽管两段代码都具有相同的结构,
SeqExtensions
不会编译。F#突出显示单词
Seq
,并返回错误“类型'Seq'未定义”

如何在Seq module上创建静态扩展方法?

要扩展F#module,只需创建另一个同名模块:

module Seq =
    let myMap f s = seq { for x in s do yield f x }

Seq. // see your stuff here alongside normal stuff
请参阅:并注意
内部扩展
可选扩展
之间的区别。