Re: [RFC] User Defined Operator Overloads (v0.6)

From: Date: Wed, 22 Dec 2021 00:47:44 +0000
Subject: Re: [RFC] User Defined Operator Overloads (v0.6)
References: 1 2 3 4 5 6 7 8 9 10 11 12 13  Groups: php.internals 
Request: Send a blank email to internals+get-116712@lists.php.net to get a copy of this message
On Tue, 21 Dec 2021 at 23:20, Jordan LeDoux <jordan.ledoux@gmail.com> wrote:
>
>
>
> On Tue, Dec 21, 2021 at 5:47 AM Andreas Hennings <andreas@dqxtech.net> wrote:
>>
>> I see the "Implied Operators" section.
>> I assume this means that a new instance will be created, and stored on
>> the same variable, _if_ the original operator is written in an
>> immutable way (which it should be)?
>>
>> E.g.
>>
>> $money = new Money(5);
>> $orig = $money;
>> $m2 = $money + new Money(2);
>> assert($money === $orig);  // Checking object identity.
>> assert($m2 !== $orig);
>> $money += new Money(1);  // Equivalent to $money = $money + new Money(1);
>> assert($money !== $orig);
>> assert($orig->amount() === 5);
>>
>> I think we need a strong recommendation to implement operators as immutable.
>
>
>  Yes. The documentation for operator overloads should be much larger than this RFC, and if this
> passes my focus for the rest of 8.2 will be on two things:
>
> - Working on a few smaller follow up RFCs (sorting/ordering enum, polymorphic handler
> resolution)
> - Working to help on the documentation of this feature
>
> All of the examples in the documentation should be for immutable implementations, and there
> should be an explicit recommendation for immutable implementations as well. With operators, mutable
> versions are created with the operators under the "Implied" section instead of by creating
> an immutable implementation of the operator itself.

Right. But even for the "implied" operators, I would say "mutable"
should refer to the variable, but not to the object.
This is what I tried to communicate with the code example.

>
> Jordan


Thread (45 messages)

« previous php.internals (#116712) next »