3
我正在嘗試爲F#中的特定類實現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
這並不編譯。我收到以下錯誤:「在成員定義中出現'意外的關鍵字',並且我得到了」可能的不正確的縮進...「警告。我不確定問題是什麼,因爲在我看來,上述如何接口一般是F#中實現。爲什麼上面沒有編制?
在更高版本的F#中已經有一些工作可以改進錯誤報告。這似乎是一個很好的候選人。也許發佈這個問題上:github.com/microsoft/visualfsharp/issues? – FuleSnabel