Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109396 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 9785 invoked from network); 28 Mar 2020 18:19:23 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 28 Mar 2020 18:19:23 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6940D1804D8 for ; Sat, 28 Mar 2020 09:44:49 -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=-2.1 required=5.0 tests=BAYES_00,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-f68.google.com (mail-lf1-f68.google.com [209.85.167.68]) (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 ; Sat, 28 Mar 2020 09:44:48 -0700 (PDT) Received: by mail-lf1-f68.google.com with SMTP id z23so10422752lfh.8 for ; Sat, 28 Mar 2020 09:44:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=lFLPiyTg04IR15Ez7VyuSrzFNmX3I0K195BktAEBUe0=; b=Pm+5pz7eZugrG8XlQG8mR05cOW70EgiXyR4eQszmhXEASUhhVqWkIvy7h+4IG4vPEO n3VjRwJL5zf27gveoZzHX3Mc8PTvBonxhN+5aTNNQbx8QwA9JWm+ibenxxapXL6ZFlyG OzjevgBzcfquCAIS+wwjPeI9E4k8DC7IJHoVRuAPI1VdRD7kbyo/kGbIz5gtMl27WA7A Q1+YjVP71/wBlym2mjR90f/ur1oZCg3rCWy+3c3CCxn8KgTSXTGzCccYwBoUnWDd1eVZ T0vBN8nc1k0x/mgcm94HhiOf9xNdC0oqG0kdktcXPUfqViOjzmxCjWalYpgMBqyCAfbi M/0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=lFLPiyTg04IR15Ez7VyuSrzFNmX3I0K195BktAEBUe0=; b=mW2gOwXTNVHoz+TX8eNFd9w4aXLah9Y/UPyphGfOdK3NYZKwsR3w+72rf7m5XCHRjJ XXIDN6VfmdvY814gtq6TAz7V0Pko3kotdTtnND0h5w1RlOppyaSE4AShInju77NlnEHE uBrpHE9pPhLB//x3BvZhGCoVoRpjr0gim61ZzbvXHQytmrLfbCaQzle+6CJwSr2Jb2L8 bqXHfFF0WMXKopBgWN3WcvuJxXRekPRlUD4+AQpvW7LNGn66rA3dnedSlhY91CwMZFS/ v4fE3Cc3V40Rc5nAS5wMBmqIdKSi37xBsrv7vnylXsnj0fZAtURIxXTRWiDZkNJ2d9YG YZtg== X-Gm-Message-State: AGi0Pubc1U6NGbFM0ECQXg1biYQjySJxwTnZ8+alCU4CjaZ6xtpGYiX5 z/14bGELuCAcRMzfMRHThYLMP3mB45MVaLLTK/o= X-Google-Smtp-Source: APiQypJkRmj0PmUHBIbRWWwf1GxpBp6CmIA1LJP1jxH79wusprxzfVM9y/tKsFFzXjWTnDU9dqp9hNLWpjuJSmWHZZA= X-Received: by 2002:ac2:5146:: with SMTP id q6mr3115387lfd.81.1585413884956; Sat, 28 Mar 2020 09:44:44 -0700 (PDT) MIME-Version: 1.0 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> In-Reply-To: <07f176a5c2ff0338cb67c9755bf37af6dcc2d465.camel@schlueters.de> Date: Sat, 28 Mar 2020 17:44:28 +0100 Message-ID: To: =?UTF-8?Q?Johannes_Schl=C3=BCter?= Cc: "Christoph M. Becker" , Arnold Daniels , PHP internals , =?UTF-8?Q?Jan_B=C3=B6hmer?= Content-Type: multipart/alternative; boundary="0000000000006f567d05a1ecefa0" Subject: Re: [PHP-DEV] [VOTE] Userspace operator overloading From: nikita.ppv@gmail.com (Nikita Popov) --0000000000006f567d05a1ecefa0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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. Becker wrote: > > On 28.03.2020 at 15:57, Johannes Schl=C3=BCter wrote: > > > > > On March 28, 2020 1:25:11 PM GMT+01:00, "Christoph M. Becker" < > > > cmbecker69@gmx.de> 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. > > > > > > 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. > > > > Fair enough. But maybe Python, where userland operator overloading > > works similar to the proposal at hand, is? :) > > It doesn't: > > > Python 3.6.9 (default, Nov 7 2019, 10:44:02) > [GCC 8.3.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> class A: > ... def __add__(self, other): > ... print("add") > ... > >>> 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. Because Python uses instance methods for operator overloading, it has to use pairs of methods like __add__ and __radd__ to handle commuted variants. If you want to implement a commutative add, you need to implement both __add__ and __radd__. If __add__ is not implemented or returns NotImplemented, then __radd__ will be called. 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. Regards, Nikita --0000000000006f567d05a1ecefa0--