Re: [VOTE] Userspace operator overloading
| From: | Christoph M. Becker | Date: | Sat, 28 Mar 2020 12:25:11 +0000 |
| Subject: | Re: [VOTE] Userspace operator overloading | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109382@lists.php.net to get a copy of this message | ||
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.
> With class inheritance, you also don't want to rely on the order of the
> operands. If you're the creator of a library, that's out of your hands. If
> both the parent and child class overwrite the operation, and both accept
> the operands, then the child class should always be used.
If a subclass overrides the operation, it would have to conform to
parameter type contravariance, so it would always be used, anyway.
[1]
<https://github.com/php/php-src/blob/php-7.4.4/Zend/zend_operators.h#L934-L937>
--
Christoph M. Becker