为什么 F# 中的这个接口实现不编译?

Why does this interface implementation in F# not compile?

我正在尝试为 F# 中的特定 class 实现 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

这不编译。我收到以下错误:"Unexpected keyword 'with' in member definition'. Also, I get a "Possible incorrect indentation ...”警告。我不确定问题出在哪里,因为在我看来,以上是 F# 中接口通常是如何实现的。为什么以上不编译?

好吧,我可以自己回答这个问题。我没有把 member 放在实现的前面。交换

this.Equals other = this.Equals other

member this.Equals other = this.Equals other

一切顺利。事实上,编译器概述了 "with" 关键字作为问题让我失望。