如何创建扩展方法(F#)?

如何创建扩展方法(F#)?

问题描述:

如何在F#中创建扩展方法,例如,像这样的C#扩展:

How do I create an extension method in F#, for example, like this C# extension:

    public static string Right(this string host, int index)
    {
        return host.Substring(host.Length - index);
    }

对于可从F#调用的F#扩展名:

For an F# extension that can be called from F#:

type System.String with
  member x.Right(index) = x.Substring(x.Length - index)

请注意,从Beta 1开始,这不会产生与C#兼容的扩展方法.

Note that as of Beta 1, this doesn't result in a C#-compatible extension method.

要生成C#可见的扩展方法(但不能用作F#中的扩展方法),请参见Brian编辑中指向原始帖子的链接.

For generating extension methods visible from C# (but not usable as extension methods in F#), see the link in Brian's edit to the original post.