From: Sean O'Dell Date: 2003-10-02T04:42:48+09:00 Subject: Re: Making == symmetric? Marcin 'Qrczak' Kowalczyk wrote: > W li�cie z �ro, 01-10-2003, godz. 19:31, Sean O'Dell pisze: > > >>My own opinion on this is: == is like asking one object if it's equal to >>another. I know that particular operator is supposed to provide a >>balanced equality test, but I only thought of == that way early in my >>programming career; that notion has long since been replaced with the >>notion that one side is asking if the other side is equal, and switching >>things around will yield different results. Because of this, the way >>Ruby handles == always made perfect sense to me. > > > This is like saying "my programming model is unable to describe the > reality accurately, so let's change the reality to make it fit the > model". The notion of a symmetrical equality test is an abstract concept. In reality, whether it's Ruby or anything else, testing is NOT symmetrical. If you compare two objects in real life, you have to compare one to the other by first observing one, then the second object. You can decide to either observe in one pass (object A first, then object B) or can observe a second time (object B first, then object A) and decide if they are equal. You can make as many passes as you want, or you can just make one pass. If you say: if (a == b) then ... then you are accepting one pass. If you say: if (a == b and b == a) then ... then you are demanding a second pass and trying to get a better idea if they are equal or not. Although I want to know if A and B are equal, I accept that the equality test is one object comparing itself to another. This is acceptable to me, and if I want more, I can override the == method or perform more tests to get the result I want. > For me this is simply a shortcoming in the traditional single-dispatched > OO way of thinking, that it can't make the meaning of == depend on the > types of both arguments. > > I don't say that the language should enforce == to be symmetric. I say > that the language should allow to define a symmetric == with the desired > meaning. But it does. You can very easily override == just as I described in my example code. You can make == anything you want it to be, for any class, existing or not. > So I'm a fan of generic functions. Unfortunately they don't quite fit > Ruby (or Ruby doesn't quite fit my programming model) - I don't know if > emulating them in Ruby is worth the effort. I'm just glad Ruby *lets* us override and emulate ... to our heart's content, it lets us. =) Sean O'Dell