On Tue, 21 Dec 2021 at 23:20, Jordan LeDoux <jordan.led...@gmail.com> wrote: > > > > On Tue, Dec 21, 2021 at 5:47 AM Andreas Hennings <andr...@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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php