What is the best way to tackle this problem?
Here is a simple class hierarchy in F#:
[<AbstractClass>]
type BaseClass (name: string) =
member this.Name with get() = name
type FooClass (n, i: int) =
inherit BaseClass (n)
member this.Num with get() = i
type BarClass (n) = inherit BaseClass (n)
I will need to add to this hierarchy quite a lot as the project advances.
I need to equate instances of these classes by checking if they are the same type and have the same value for Name (and also the same value for Num if its a FooClass), so ReferenceEquals won’t cut it. What is the best way to do this? Should I make BaseClass inherit IComparable and if so, how should I then deal with the types that inherit from BaseClass and have extra fields to check? Another way of doing it would be to make an IComparer class that checks for every different class but I really want the code to be in the appropriate class rather than in a comparer
I believe overriding equals on the base class, and then overriding equals on each sub class by leveraging the base class implementation is the standard approach in .NET OO: