我目前正在 F# 中实现 Spec 框架,我想隐藏我的 Equals、GetHashCode 等方法should
类型,以便 API 不会因这些而混乱。
我知道在 C# 中,这是通过让类实现如下接口来完成的:
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# 中做同样的事情:
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
在我的类型中实现它:
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()
但没有成功。
我还尝试重写我的类型中的方法并以这种方式添加属性,但这也没有达到目的。
所以问题仍然存在,如何清理我的 API ?
Edit:
感谢您的帮助(见下文),我能够解决我的问题。
总之,.Equals
and .GetHashCode
可以通过隐藏[<NoEquality>]
[<NoComparison>]
但这也会改变语义。
通过 EditorBrowsable 属性隐藏不起作用。
拥有干净的 API 并且仍然能够重载方法的唯一方法是使这些方法成员静态。
可以通过浏览我的项目找到生成的类FSharp规格 http://github.com/thlorenz/FSharpSpec.
可以找到有问题的类型here. http://github.com/thlorenz/FSharpSpec/blob/master/src/FSharpSpec/should.fs
感谢所有帮助我解决这个问题的人。
干杯...