Hi,
Christoph M. Becker wrote:
On 28.03.2020 at 10:22, Arnold Daniels wrote:
This issues become even more apparent when sequencing operations like `$a +
$b + $c - $d`. Trying left, then trying right, will make it very difficult
to determine the outcome of such a statement.
The arguments against type hinting for operator methods, assume the "try
left/right" method. Instead, type hinting should be applied to determine
which method should be used. If both or neither methods are applicable, an
error must be thrown.
This "try left/right" approach is how operator overloading works for
internal classes[1], and apparently, it works quite well, as long as it
is not overused.
I think “as long as it is not overused” are the key words there. We have
a very limited number of internal classes with operator overloading
right now, and the authors know about what other such classes exist and
therefore can design with them in mind. But once any userland PHP class
can use operator overloading, you could start seeing uses of operators
where the left- and right-hand side are classes from unrelated libraries
whose authors were not aware of eachother, and that could create strange
problems when one or the other library is updated with operator overload
handling.
Consider Arnold's example:
$a + $b + $c - $d
We are not constraining operator overloading to just be for number-like
objects, it can in principle be used for absolutely anything. $a here
might have an overload that works for any right-hand-side object, but
then once $b adds its own similar overload in a new version, the code no
longer does the same thing when it's flipped around ($b + $a), violating
the normal expectation that `+` is commutative, and that's before
considering what $c and $d might do!
Thanks,
Andrea
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php