views:

239

answers:

1

According to this post, F# supports extension methods on object instances and static classes. For example:

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

open ExtensionFSharp.CollectionExtensions

If I type System.Linq.Enumerable., the static method RangeChar appears in my Intellisense window.

I want to add a static method, for_alli, to the Seq module. I've modified the following code above as follows:

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)

Although both snippets of code have the same structure, SeqExtensions doesn't compile. F# highlights the word Seq and returns the error "The type 'Seq' is not defined".

How do I create static extension methods on Seq module?

+6  A: 

To extend an F# module, just create another module with the same name:

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

Seq. // see your stuff here alongside normal stuff
Brian
I knew it had to be simple, thank you :)
Juliet