Re: Operator overloading for userspace objects
| From: | Ben Ramsey | Date: | Wed, 29 Jan 2020 00:06:52 +0000 |
| Subject: | Re: Operator overloading for userspace objects | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108304@lists.php.net to get a copy of this message | ||
> On Jan 28, 2020, at 17:55, Michael Cordover <mjec@mjec.net> wrote:
>
> 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.
I just remembered this extension from a few years back. It’s no longer maintained, but it might
help your efforts: https://github.com/php/pecl-php-operator
Cheers,
Ben
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc
Attachment: [application/pgp-signature] Message signed with OpenPGP signature.asc