4 ms·
A solution was suggested by the grandparent: make `a==b` call both equality methods. Dunno if you raise or just return false if they disagree.
by repsilat 8y ago
A solution was suggested by the grandparent: make `a==b` call both equality methods.
Dunno if you raise or just return false if they disagree.
- munificent 8y agoI was working on a hobby language a few years ago and trying to figure out a way to handle equality to avoid these problems. One solution I considered was: 1. Ask the LHS for its equality function. 2. Ask the RHS for its equality function. 3. If the two return different functions (non-identical), the objects are not equal. 4. Otherwise, return the result of invoking the function. The first two steps are polymorphic, so objects can define interesting equality semantics with whatever policy makes sense for them. But step 3 ensures that the two objects must agree on that policy before you can compare them.
- repsilat 8y ago> One solution I considered It sounds pretty reasonable, but I have no idea how "sounds reasonable" correlates with "is helpful in practice". (I think one of the commenters in the linked page might have raised that point too.) Fulfilling axioms of equality definitely does sound useful for being able to write code, though -- having symmetry, not having to think about whether you need to write `a==b` or `b==a`, seems like it'd lighten the cognitive load. I suppose also when the objects disagree about equality you also have some license to say "this may not do quite what you expect" and do your best to make the most of it. > I was working on a hobby language a few years ago Hah, you're famous enough that we've all probably heard of the language if it has seen the light of day ;-). My own hobby language has no methods or dynamic dispatch of any kind, haha. It's a little stressful to have to come up with some equality rule that works for essentially every use-case, though -- I don't think it's sufficient to enumerate the data types in the language, I really have to go over all the ways I think they could be used :-/. Oh well, it's fun at least.