It looks like you are just defining let-bound functions within a module, which can't be overloaded. If you want to define extension methods on a type, do it like so:

module MyExts
open System
open System.Text.RegularExpressions


type String with
  member input.WordCount() =
    input.Split([|' '; '.'; '?' |], StringSplitOptions.RemoveEmptyEntries).Length


  member input.WordCount(test:string)=
    input.Split([|' '; '.'; '?' |], StringSplitOptions.RemoveEmptyEntries) |>
    Array.filter(fun x -> Regex.IsMatch(x, test)) |>
    Array.length 


let testCount = "This is a sentence".WordCount()

Note that the extension methods are defined exactly as normal methods on a type would have been.
<system.runtime.compilerservices.extensionattribute><system.runtime.compilerservices.extension><system.runtime.compilerservices.extension></system.runtime.compilerservices.extension></system.runtime.compilerservices.extension></system.runtime.compilerservices.extensionattribute>
By on 5/3/2010 12:32 PM ()
Thanks. Is there a way to make this extension visible in C#?
By on 5/3/2010 12:38 PM ()
As I understand it, F# and C# encode extension methods in different ways. You can write either type of extension method in F#, but the resulting code will only be seen as an extension method in one language or the other. By contrast, extension methods written in C# can be used from F#. If you want to write overloaded extension methods in F# which are visible from C#, here's one way to do it:

namespace Extensions


open System
open System.Runtime.CompilerServices
open System.Text.RegularExpressions


[<Extension>]
[<AbstractClass>]
[<Sealed>]
type StringExts =
  [<Extension>]
  static member WordCount(input:string) =
    input.Split([|' '; '.'; '?' |], StringSplitOptions.RemoveEmptyEntries).Length


  [<Extension>]
  static member WordCount(input:string, test:string)=
    input.Split([|' '; '.'; '?' |], StringSplitOptions.RemoveEmptyEntries) |>
    Array.filter(fun x -> Regex.IsMatch(x, test)) |>
    Array.length 


In C#, you then need to reference the resulting DLL and then open the Extensions namespace to be able to see the extension methods. In F# you can call these methods like you would call any static method, but you can't call them as extension methods.

As Brian mentioned, another alternative is to use modules as you initially tried to do, but to use one module per overload since there's no way to overload let-bound functions within a single module.
By on 5/3/2010 1:02 PM ()
Thank you. Thats what i was looking for.
By on 5/3/2010 1:21 PM ()
Do what you originally did, but use a separate module for each overload.

See also

[link:stackoverflow.com]
By on 5/3/2010 12:58 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper