Re: [RFC] User-defined object comparison
| From: | Stanislav Malyshev | Date: | Tue, 03 Jul 2018 20:14:38 +0000 |
| Subject: | Re: [RFC] User-defined object comparison | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-102561@lists.php.net to get a copy of this message | ||
Hi!
> In other words, $a == $b must not work if $a->__equals and $b->__equals are
> two different functions
>
> So effectively it means we only call __equals if both sides are instances
> of the same class?
We can just define that == calls __equals on the left. That's, for
example, what Python does. This would produce a weird consequence of $a
== $b and $b == $a not being the same, but that's what you get for
overloading operators. That's why overloading operators is not always
the best idea, because of disconnect between perceived semantics (math
equality operator, which is symmetric, reflexive and transitive) and
actual semantic (which is not guaranteed to have any of the math
equality properties).
In any case, "the same class" would be too restrictive, as there's
inheritance, so we should support LSP. But then again, if $a and $b are
in different places on inheritance hierarchy, we can can non-symmetric
operator. This is kinda deep problem that is not really working well in
most Java code, for example. See:
https://www.artima.com/pins1ed/object-equality.html
(for reference,
Martin Odersky is the guy who did Java generics, and designed Scala, so
he knows a quite bit about OO things :)
--
Stas Malyshev
smalyshev@gmail.com