Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109397 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 22089 invoked from network); 28 Mar 2020 18:47:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2020 18:47:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9E7B91804B8 for ; Sat, 28 Mar 2020 10:13:08 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS31103 84.19.160.0/19 X-Spam-Virus: No X-Envelope-From: Received: from mail.toneristzuen.de (mail.toneristzuen.de [84.19.169.162]) by php-smtp4.php.net (Postfix) with ESMTP for ; Sat, 28 Mar 2020 10:13:07 -0700 (PDT) Received: from [192.168.2.46] (ppp-188-174-60-166.dynamic.mnet-online.de [188.174.60.166]) by mail.toneristzuen.de (Postfix) with ESMTPSA id EBE1F49A14; Sat, 28 Mar 2020 18:13:06 +0100 (CET) Date: Sat, 28 Mar 2020 18:13:04 +0100 User-Agent: K-9 Mail for Android In-Reply-To: References: <003701d6013c$9afe9750$d0fbc5f0$@gmx.de> <7a83f950a31d94d5ff2307ac8219db3b7b6482b6.camel@schlueters.de> <12ad7c71-8958-7742-12c4-e83e359c8186@gmx.de> <3B71F74D-8142-48FB-9660-835B08D1DDDD@schlueters.de> <705aba69-8c17-f882-19fd-6f41a2c2ca25@gmx.de> <07f176a5c2ff0338cb67c9755bf37af6dcc2d465.camel@schlueters.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable To: Nikita Popov CC: "Christoph M. Becker" ,Arnold Daniels ,PHP internals ,=?ISO-8859-1?Q?Jan_B=F6hmer?= Message-ID: Subject: Re: [PHP-DEV] [VOTE] Userspace operator overloading From: johannes@schlueters.de (=?ISO-8859-1?Q?Johannes_Schl=FCter?=) On March 28, 2020 5:44:28 PM GMT+01:00, Nikita Popov wrote: >On Sat, Mar 28, 2020 at 5:28 PM Johannes Schl=C3=BCter > >wrote: > >> On Sat, 2020-03-28 at 17:05 +0100, Christoph M=2E Becker wrote: >> > On 28=2E03=2E2020 at 15:57, Johannes Schl=C3=BCter wrote: >> > >> > > On March 28, 2020 1:25:11 PM GMT+01:00, "Christoph M=2E Becker" < >> > > cmbecker69@gmx=2Ede> wrote: >> > > >> > > > This "try left/right" approach is how operator overloading >works >> > > > for >> > > > internal classes[1], and apparently, it works quite well, as >long >> > > > as it >> > > > is not overused=2E >> > > >> > > The fact that it works in one or two cases as an implementation >> > > detail where the full implementation is controlled by a single >> > > group (internals) is no indication for it to work at large=2E >> > >> > Fair enough=2E But maybe Python, where userland operator overloading >> > works similar to the proposal at hand, is? :) >> >> It doesn't: >> >> >> Python 3=2E6=2E9 (default, Nov 7 2019, 10:44:02) >> [GCC 8=2E3=2E0] on linux >> Type "help", "copyright", "credits" or "license" for more >information=2E >> >>> class A: >> =2E=2E=2E def __add__(self, other): >> =2E=2E=2E print("add") >> =2E=2E=2E >> >>> a =3D A() >> >>> a + 1 >> add >> >>> 1 + a >> Traceback (most recent call last): >> File "", line 1, in >> TypeError: unsupported operand type(s) for +: 'int' and 'A' >> > >It does=2E > >Because Python uses instance methods for operator overloading, it has >to >use pairs of methods like __add__ and __radd__ to handle commuted >variants=2E >If you want to implement a commutative add, you need to implement both >__add__ and __radd__=2E If __add__ is not implemented or returns >NotImplemented, then __radd__ will be called=2E > >So, as Christoph has said, this is indeed the same system as what is >being >proposed here, with the difference that we do not need two separate >methods >per operator, but can use a static method instead=2E If we use two methods as well it is a saner design=2E I won't like it, but= lot better than the current one=2E johannes