Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116855 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89880 invoked from network); 10 Jan 2022 16:40:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Jan 2022 16:40:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B366D1804A7 for ; Mon, 10 Jan 2022 09:48:51 -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=-0.7 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 10 Jan 2022 09:48:48 -0800 (PST) Received: by mail-lf1-f44.google.com with SMTP id m1so13912651lfq.4 for ; Mon, 10 Jan 2022 09:48:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=faRvzSWA2RyX5wK/tn1XMMmgbXzhdkCjYzwHp5f1Oxs=; b=n/oMXlfjYXQdyZhi+axbMH1uT3ygM/BIFR7IdW7vf6N0Q7s8TieKzvkNy3n1UOIX65 t1n/GocKmFDjcSyD71BuA9Q/ArdOxDgGsszYfaZoGHjjPyvvRtYCxbtZXWoH6wFz7GJN noStIjql7DISKetLKB5+ahb3WZrJpEsTsia5jT20saZDPwTx7KQzG27soyDODThwtj/A ZRzfzVfC79CedwupGPWCgBcYBse4i2w3rLqnQrhLL8ayperI/KIvCTqtxlmJGgk4Fz95 wiucnY7NgkXyvNErS7xLpgck9OiQ5L3Zh6trTl2SMwsZCbc5JwwsLUY03EckupFF+596 S8gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=faRvzSWA2RyX5wK/tn1XMMmgbXzhdkCjYzwHp5f1Oxs=; b=x2SiSG79wiIc8fP6Tw+JJGXMvm+waj8Rgn8IF9zlOwBhOVhV29RvJKZFkqfFEyheTe Vcn5VjV7x1s6wh7DKsL+D/QngY1jz1SKXW4xI1KY+PDEZmyKrnlCldrsJcs9yd0SSwK9 M63hqeXt2Sv+BCxZUR/PF8BUSuSusVV38lk7qsTv6gFFq2s5++BIu7LNBbQpXlNOBuJZ NaDczZDxu40mzh9vESV0vfY9zFZpsX3KzeItQEvDufX0Q3CJEGP1dOAUCV45Y1DeTm+0 hzqMmLVSvJKpwKsOSeo1d/nERgYFF12zCgDwpi4SFyKg5ppXakDMxfBl8QGQ+Ea3SqEH WyZA== X-Gm-Message-State: AOAM5311bEuc2fjg/cNj2kAGoA6N7IYBeh2FCe+mHCiZEbf7EfFGisWq 61l4JAGs02l6i8qp/xSauai1l6+oLQUnKOav7A== X-Google-Smtp-Source: ABdhPJxeHdtaF3Ib5Mw0hTKa3jBKHS+1fg44z2QOAtLu21/gG5MBLDT32EYtUgtx99UnnApUAtDNUdhOV/oM+p3qY68= X-Received: by 2002:a05:6512:230f:: with SMTP id o15mr612234lfu.348.1641836926549; Mon, 10 Jan 2022 09:48:46 -0800 (PST) MIME-Version: 1.0 References: <1804801.tdWV9SEqCh@come-prox15amd> In-Reply-To: <1804801.tdWV9SEqCh@come-prox15amd> Date: Mon, 10 Jan 2022 18:48:55 +0100 Message-ID: To: jordan.ledoux@gmail.com Cc: =?UTF-8?Q?C=C3=B4me_Chilliet?= , internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000c941fb05d53df295" Subject: Re: [PHP-DEV] [VOTE] User Defined Operator Overloads From: guilliam.xavier@gmail.com (Guilliam Xavier) --000000000000c941fb05d53df295 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Jordan, Many thanks for all your replies. But did you miss C=C3=B4me's mail (maybe because you weren't direct recipient)? Anyway, here it is again: On Tue, Jan 4, 2022 at 10:02 AM C=C3=B4me Chilliet wrote= : > Hello, > > From the RFC: > > > If the left operand produces a TypeError due to the parameter types > listed in the implementation, the operation is not retried with the right > operand 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 / n= ew 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 > TypeError? So it means what I was able to do for floats, cannot be done f= or > my new classes afterwards? This is inconsistent I think. It also means it > is not possible to extend any existing class with operators interacting > with a new class, meaning you can only use operators among classes aware = of > each other, 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) {} > > > > public operator *(Matrix $other, OperandPosition $operandPos): Numb= er > > { > > 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 > always call $matrix1->{'*'}($matrix2, LeftSide) and never the other way > around. > This means that $operandPos is useless in all cases where operators are > only typed againts the same type, and points to this design solution bein= g > wrong. > > There is =C2=ABWhy not interfaces?=C2=BB in the FAQ, does that mean that = operators > cannot be added in interfaces? This is not stated clearly in the RFC. > > Also, it is not clear if operator overload will affect comparison > operations 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= in > an array? > Which of these internal functions use =3D=3D and which use =3D=3D=3D, whi= ch is not > overloadable? > Does implementing operator <=3D> allows sorting of arrays containing my > objects? > > C=C3=B4me > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > I too would be interested in your answers to those. Best regards, --=20 Guilliam Xavier --000000000000c941fb05d53df295--