On Fri, Apr 5, 2024 at 1:00 PM Tim Düsterhus <t...@bastelstu.be> wrote:

> Hi
>
> On 4/5/24 21:42, Saki Takamachi wrote:
> > The only solution I can think of at the moment is to impose the
> constraint that
> > when computing operator overloading, if the operands are both objects,
> they must
> > be of the exact same class.
>
> Even that would allow for confusing behavior:
>
>      class MyNumber extends Number {
>          private $importantMetadata;
>
>          public function doSomething() {
>              $this->importantMetadata = random_int(1, 100);
>          }
>      }
>
>      $a = new MyNumber(5);
>      $a->doSomething();
>      $b = new MyNumber(10);
>      $b->doSomething();
>
>      $what = $a + $b;
>
> What should be the value of $what->importantMetadata be? The property is
> private, so as a user adding two of MyNumber would not even be able to
> manually fix it up with the correct value, thus requiring the addition
> of a "fixup" method that fixes the internal state of the object,
> possibly even lacking the necessary information to properly fix up the
> state, because it does not know which operations lead to the state.
>
> Best regards
> Tim Düsterhus
>

That is an absurd example. Why would anyone use inheritance for that class
design? If what you are arguing is "if you look at use cases where
composition is clearly the correct choice then inheritance causes
problems", then I'm not sure what the point of the discussion is.

Jordan

Reply via email to