如何隐藏在F#的方法呢? [英] How can I hide methods in F#?

查看:113
本文介绍了如何隐藏在F#的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施的F#规格书框架,我想隐藏的Equals,GetHash code的方法等等。我的输入,从而使API是不是这些混乱。

I am currently implementing a Spec framework in F# and I want to hide the Equals, GetHashCode etc. methods on my should type, so that the API is not cluttered with these.

我知道在C#中它是通过使类实现这样的接口完成的:

I know in C# it is done by making the class implement an interface like this:

using System;
using System.ComponentModel;

public interface IFluentInterface
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    bool Equals(object other);

    [EditorBrowsable(EditorBrowsableState.Never)]
    string ToString();

    [EditorBrowsable(EditorBrowsableState.Never)]
    int GetHashCode();

    [EditorBrowsable(EditorBrowsableState.Never)]
    Type GetType();
}

我试过在F#中做同样的:

I tried doing the same in F#:

type IFluentInterface = interface

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract Equals : (obj) -> bool

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract ToString: unit -> string

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract GetHashCode: unit -> int

    [<EditorBrowsable(EditorBrowsableState.Never)>]
    abstract GetType : unit -> Type 
end

在我喜欢的类型实现了它:

Implemented it in my type:

        interface IFluentInterface with
        member x.Equals(other) = x.Equals(other)
        member x.ToString()    = x.ToString() 
        member x.GetHashCode() = x.GetHashCode()
        member x.GetType()     = x.GetType() 

但没有成功。

我也试图覆盖我喜欢的类型,并添加属性的方式方法,但没有做的伎俩无论是。

I also tried to override the methods in my type and adding the attribute that way, but that didn't do the trick either.

所以,接下来的问题是,我该怎么收拾我的API?

So the question remains, how can I clean up my API ?

编辑:

感谢帮助(见下文),我是能够解决我的问题。

Thanks to the help (see below) I was able to solve my problem.

总之, .Equals .GetHash code 可以通过 [&LT; NoEquality&GT;] [&LT; NoComparison&GT;。但也将改变的语义

In summary, .Equals and .GetHashCode can be hidden via [<NoEquality>] [<NoComparison>] but that will also change the semantics.

通过EditorBrowsable隐藏属性不起作用。

The hiding via EditorBrowsable attributes does not work.

有一个干净的API,仍然能够重载方法的唯一方法是使这些方法静态成员

The only way to have a clean API and still be able to overload methods is to make these method members static.

生成的类可以通过我的项目中浏览找到 FSharpSpec

The resulting class can be found by browsing inside my project FSharpSpec.

问题的类型,可以发现

The type in question can be found here.

感谢大家谁帮我解决这个问题。

Thanks to everyone who helped me solve this problem.

干杯...

推荐答案

另外,你可以设计使用封闭使用一个模块功能的另类风格库。这是写的功能的$ C $ F#中c和那么你就不会需要隐藏任何标准.NET方法通常的方式。为了完成KVB给出的例子,这里是面向对象的解决方案的例子:

Alternatively, you could design the library using an alternative style using functions enclosed in a module. This is the usual way for writing functional code in F# and then you won't need to hide any standard .NET methods. To complete the example given by 'kvb', here is an example of object-oriented solution:

type MyNum(n:int) =
  member x.Add(m) = MyNum(n+m)
  member x.Mul(m) = MyNum(n*m)

let n = new MyNum(1)
n.Add(2).Mul(10) // 'ToString' shows in the IntelliSense

写code的功能的方式可能是这样的:

The functional way of writing the code might look like this:

type Num = Num of int
module MyNum =
  let create n = Num n
  let add m (Num n) = Num (m + n)
  let mul m (Num n) = Num (m * n)

MyNum.create 1 |> MyNum.add 2 |> MyNum.mul 10

如果您键入 myNum的。,F#的智能感知将显示模块中定义的函数,所以你不会看到在这种情况下,任何噪声。

If you type MyNum., the F# IntelliSense will show the functions defined in the module, so you won't see any noise in this case.

这篇关于如何隐藏在F#的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆