Why doesn't this implementation of the interface in F # compile?
I am trying to implement IEquatable<T>
for a specific class in F #. However, when you do it in this case, I get an unexpected error.
I have the following code:
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
It doesn't compile. I am getting the following error: "Unexpected keyword" c "in member definition. The warning "Possible wrong indentation ..." also appears. I'm not sure what the problem is, as it seems to me that the above is how interfaces are generally implemented in F #. Why doesn't it compile?
+3
source to share
1 answer
Ok, I can answer the question myself. I did not put a participant before implementation. Having exchanged
this.Equals other = this.Equals other
from
member this.Equals other = this.Equals other
does everything right. The fact that the compiler spelled out the "c" keyword as the problem threw me away.
+6
source to share