Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116800 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72998 invoked from network); 4 Jan 2022 07:54:54 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 4 Jan 2022 07:54:54 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 48F261804A7 for ; Tue, 4 Jan 2022 01:02:04 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS29169 217.70.176.0/20 X-Spam-Virus: No X-Envelope-From: Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 4 Jan 2022 01:02:02 -0800 (PST) Received: (Authenticated sender: come@chilliet.eu) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 206E56001A for ; Tue, 4 Jan 2022 09:01:59 +0000 (UTC) To: internals@lists.php.net Date: Tue, 04 Jan 2022 10:01:59 +0100 Message-ID: <1804801.tdWV9SEqCh@come-prox15amd> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [VOTE] User Defined Operator Overloads From: come@chilliet.eu (=?ISO-8859-1?Q?C=F4me?= Chilliet) Hello, =46rom the RFC: > If the left operand produces a TypeError due to the parameter types liste= d in the implementation, the operation is not retried with the right operan= d and the error is instead returned immediately. This is to help developers= encounter errors in their program logic as early as possible. This feels wrong if I understand correctly. Let=E2=80=99s say I create a class A and I want to add support for "2 / new= A()", which by default does not work. I can do class A { operator /(int $other, OperandPosition $operandPos): mixed { return "example"; } } Now someone wants to build on my work, and add a class B that supports "new= A() / new B()". class B { operator /(A $other, OperandPosition $operandPos): mixed { return "example"; } } This will not work because it will first try A->{'/'}(B) that throws a Type= Error? So it means what I was able to do for floats, cannot be done for my = new classes afterwards? This is inconsistent I think. It also means it is n= ot possible to extend any existing class with operators interacting with a = new class, meaning you can only use operators among classes aware of each o= ther, most likely from the same package/library. Also, this was stated already I think but I did not see an answer, from RFC= example: > class Matrix { > public function __construct(readonly public array $value) {} > =20 > public operator *(Matrix $other, OperandPosition $operandPos): Number > { > if ($operandPos =3D=3D OperandPosition::LeftSide) { > // Count of my columns needs to match > // count of $other rows > } else { > // Count of my rows needs to match > // count of $other columns > } > } > } The second branch of the if is dead code, as $matrix1 * $matrix2 will alway= s call $matrix1->{'*'}($matrix2, LeftSide) and never the other way around. This means that $operandPos is useless in all cases where operators are onl= y typed againts the same type, and points to this design solution being wro= ng. There is =C2=ABWhy not interfaces?=C2=BB in the FAQ, does that mean that op= erators cannot be added in interfaces? This is not stated clearly in the RF= C. Also, it is not clear if operator overload will affect comparison operation= s used in core functions such as in_array, sort and so on. Does implementing operator =3D=3D allows using in_array to find an object i= n an array? Which of these internal functions use =3D=3D and which use =3D=3D=3D, which= is not overloadable? Does implementing operator <=3D> allows sorting of arrays containing my obj= ects? C=C3=B4me