我正在尝试为F#中的特定类实现IEquatable<T>
。但是,在这个特殊情况下这样做时,我会收到意外错误。
我有以下代码:
type Foo private (name : string) =
member this.Name = name
member this.Equals (other : Foo) = this.Name = other.Name
override this.Equals other =
match other with | :? Foo as foo -> this.Equals foo | _ -> false
override this.GetHashCode () = this.Name.GetHashCode ()
interface IEquatable<Foo> with
this.Equals other = this.Equals other
这不编译。我收到以下错误:“带有'成员定义'的意外关键字'。另外,我收到了”可能不正确的缩进...“警告。我不知道问题是什么,因为在我看来,上面接口通常是如何在F#中实现的。为什么以上不能编译?
答案 0 :(得分:6)
好吧,我可以自己回答这个问题。我没有把成员放在实施的前面。交换
this.Equals other = this.Equals other
与
member this.Equals other = this.Equals other
让一切顺利。编译器将“with”关键字概述为问题的事实让我失望。