Seq模块上的静态扩展方法

时间:2009-03-22 18:39:18

标签: f# extension-methods

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

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

open ExtensionFSharp.CollectionExtensions 

如果我输入System.Linq.Enumerable.,静态方法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模块上创建静态扩展方法?

1 个答案:

答案 0 :(得分:46)

要扩展F#模块,只需创建另一个具有相同名称的模块:

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

Seq. // see your stuff here alongside normal stuff
相关问题