根据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
将出现在我的Intellisense窗口中 .
我想在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
并返回错误"The type 'Seq' is not defined" .
How do I create static extension methods on Seq module?
1 回答
要扩展F#模块,只需创建另一个具有相同名称的模块: