Re: The future of objects and operators
| From: | Larry Garfield | Date: | Fri, 13 May 2022 14:26:37 +0000 |
| Subject: | Re: The future of objects and operators | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117728@lists.php.net to get a copy of this message | ||
On Fri, May 13, 2022, at 9:05 AM, Rowan Tommins wrote:
> On 6 May 2022 23:16:37 BST, Jordan LeDoux <jordan.ledoux@gmail.com> wrote:
>>I'm not sure at this point how voters think objects and operators should
>>work together into the future. I'd like to see if anyone is willing to have
>>high-level discussion about the ideas, instead of picking at the
>>implementation or details of a particular RFC.
>
>
> Hi Jordan,
>
> I like Larry's "4 levels", but I've been thinking that there's some
> existing functionality in PHP which takes a different direction: rather
> than overloading *operators*, the language lets you overload
> *behaviour*. We have magic methods like __get and __call, and
> interfaces like Iterator, Countable, and ArrayAccess. Some hook into a
> particular function, or even operator, but they're generally described
> in terms of what they do, not what they look like, if you see what I
> mean.
>
> From that point of view, overloading comparison and equality
> *behaviour* makes sense - it could affect not just the == and <=>
> operators, but things like in_array() and sort(). I think this
> distinction is more pronounced in PHP than some languages, because the
> standard library isn't "self-hosted": a sort() call doesn't literally
> compile to a call to $a <=> $b
>
> It's less obvious how that applies to mathematical operators - should
> implementing "addition" allow an object to be used with array_sum()
> perhaps? And what about deriving one operation from another, e.g.
> $foo*3 running $foo+$foo+$foo, or $foo**3 running $foo*$foo*$foo?
>
> I don't really have a conclusion here, I just wanted to throw it out
> there as a different mental model to consider.
Interesting point. I think that's the most compelling argument I've heard for using
public function __lessThan rather than operator <. I'm not sure
if it's enough to change my own preference on that front, but it's an interesting point.
Working with in_array() and sort() is a necessary feature for comparison
overloading, IMO. I'm unsure about something like array_sum(), but could be
convinced.
Hum.
--Larry Garfield