2017-06-26 62 views
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#中實現。爲什麼上面沒有編制?

回答

6

好吧,我可以回答這個問題我自己。我沒有把成員在執行前。交換

this.Equals other = this.Equals other 

member this.Equals other = this.Equals other 

使一切正常。事實上,編譯器概述了「與」關鍵字作爲問題扔了我。

+1

在更高版本的F#中已經有一些工作可以改進錯誤報告。這似乎是一個很好的候選人。也許發佈這個問題上:github.com/microsoft/visualfsharp/issues? – FuleSnabel