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

Reply via email to