根据这篇文章,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窗口中.
我想for_alli
在Seq模块中添加一个静态方法.我已经修改了上面的代码,如下所示:
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模块上创建静态扩展方法?
要扩展F#模块,只需创建另一个具有相同名称的模块:
module Seq = let myMap f s = seq { for x in s do yield f x } Seq. // see your stuff here alongside normal stuff