Re: Re: [RFC] Userspace operator overloading
| From: | Terje Slettebø | Date: | Tue, 03 Mar 2020 14:45:29 +0000 |
| Subject: | Re: Re: [RFC] Userspace operator overloading | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108824@lists.php.net to get a copy of this message | ||
Hi Rowan.
I agree that this makes sense. However, as someone else pointed out, one problem with interfaces is that they constrain the types that may be used.
We have our own Money, Percentage, PricePerMm, etc. types that we'd like to define operators for, how can you define an interface in PHP that accommodate types that are unknown to the PHP implementors?
If we had support for generics, we could have defined generic interfaces, but we don't so we can't.
Regards,
Terje
Hi Terje, I think both of your examples are compatible with the idea of grouped operators, as long as we don't constrain a type to implement all operators with the same right-hand side. ... If this operator was implemented in isolation, it would again be drifting into domain-specific language territory. (I don't personally have a problem with that, but many people vocally object to it.) However, it could easily be made part of a consistent set of arithmetic operators: On class Money: Money + Money => Money Money - Money => Money Money * int => Money Money / int => Money int * Money => Money int / Money => Error Money * Percentage => Money Money / Percentage => Money Percentage * Money => Money Percentage / Money => Error Optionally, on class Percentage: Percentage + Percentage => Percentage Percentage - Percentage => Percentage Percentage* int => Percentage Percentage / int => Percentage