[almighty] Equaler considered harmful
Thomas Mäder
tmader at redhat.com
Thu Sep 22 13:32:35 UTC 2016
Hi folks,
some time back, we have introduced the "Equaler" interface. I believe
we're going down the wrong path with this: Java got this wrong initially
and we still suffer the consequences. Equaler looks like this:
type Equaler interface {
Equal(Equaler) bool
}
There are a couple of drawbacks to this approach:
1. There can only be a single implementation of Equals for all time.
But this too restrictive: for example, I might want to include the
version field in a struct in the comparison for some cases, in other
cases I simply don't care. Generally, there can be many equivalence
relationships for a given set of objects. For all we know, we might
consider two objects equal if they have the same color.
2. You can't compose equality with this implementation
In Java, this has given rise to classes like "EqualsBuilder" that
allowed to do exactly that.
3. We can't implement equality based on interfaces
We'd have to reimplement equality for every struct (which is the
first parameter).
4. You always have to check for null before calling Equals()
So I propose to rewrite the interface to :
Equality {
Equals(left interface{}, right interface{}) bool
}
I think it would make sense that I prepare a PR to illustrate my
approach porting the current implementations in the "models" packe. A
POC shouldn't take too long.
thoughts?
/Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/almighty-public/attachments/20160922/353cc487/attachment.htm>
More information about the almighty-public
mailing list