Re: Operator overloading for userspace objects
| From: | Michael Cordover | Date: | Tue, 28 Jan 2020 23:59:50 +0000 |
| Subject: | Re: Operator overloading for userspace objects | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108303@lists.php.net to get a copy of this message | ||
On Tue, Jan 28, 2020, at 18:47, Ben Ramsey wrote:
> If you take mutation off the table, then things become easier, IMO. We
> only need two magic methods:
>
> * __toInteger(): int
> * __toFloat(): float
>
> Then, in any mathematical context, PHP could call the appropriate
> method and use the number returned in the calculation.
I don't think this is enough to make operator overloading useful, even without mutation.
For example, the result of TimeInterval(1, 'ms') + TimeInterval(3, 'days')
requires more information that we'd get out of __toInteger or __toFloat, but could still be a
useful operation to perform, and ought to return a new TimeInterval (not an int). There are many of
these tagged-number types where overloading would be helpful.
- mjec