Ale*_*kol 3 generics f# equality interface
我正在尝试为IEquatable<T>
F#中的特定类实现.但是,在这个特定的情况下这样做时,我会收到意外错误.
我有以下代码:
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
Run Code Online (Sandbox Code Playgroud)
这不编译.我收到以下错误:"带有'成员定义'的意外关键字'.另外,我收到了"可能不正确的缩进..."警告.我不知道问题是什么,因为在我看来,上面接口通常是如何在F#中实现的.为什么上面没有编译?
好吧,我可以自己回答这个问题.我没有把成员放在实施的前面.交换
this.Equals other = this.Equals other
Run Code Online (Sandbox Code Playgroud)
同
member this.Equals other = this.Equals other
Run Code Online (Sandbox Code Playgroud)
让一切都好.编译器概述了"with"关键字作为问题的事实让我失望.