Re: [VOTE] Userspace operator overloading
| From: | Christoph M. Becker | Date: | Sun, 29 Mar 2020 10:52:11 +0000 |
| Subject: | Re: [VOTE] Userspace operator overloading | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-109412@lists.php.net to get a copy of this message | ||
On 28.03.2020 at 22:21, Andrea Faulds wrote:
> Christoph M. Becker wrote:
>> (frankly, I would not have not supported __concat() at all). Also,
>> overloaded operators should be programmed defensively, i.e. they should
>> not accept arbitrary arguments (how could that even work?), but only
>> those they can handle. If implementations adhere to these "rules", I
>> don't see real issues.
>
> Consider a type implementing some kind of list. Perhaps someone would
> want to overload the + operator to mean adding an item to the list. If
> the list accepts any type of value as a valid item, then you have an
> example of an unconditional overload.
I had already considered this, and wrote immediately above the quote:
> If, for example, the + operator is overloaded to add something to a
> collection, the normal expectation that + is commutative is already
> violated. Operator overloading should definitely not be used for
> "anything", but only for those rare cases which resemble math
> operations
Thanks,
Christoph