Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109584 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 91150 invoked from network); 9 Apr 2020 16:37:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Apr 2020 16:37:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 748C41804B7 for ; Thu, 9 Apr 2020 08:05:43 -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,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-il1-f182.google.com (mail-il1-f182.google.com [209.85.166.182]) (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 ; Thu, 9 Apr 2020 08:05:42 -0700 (PDT) Received: by mail-il1-f182.google.com with SMTP id i75so10426088ild.13 for ; Thu, 09 Apr 2020 08:05:42 -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; bh=yL2rXiHBVpzxjWfWWjx4AyfIRFe/Oy4Y0W5dQ/8jozQ=; b=bphX1hVX+8o1pBIHsUApj8Zk/5whevp8jWPQlTbF9ZLu8q6k7rg7MXsJQdUfp/sJT1 1hkDw/d6uQl/clxUy8aSDOgcwEav8AP4lkOPH1ayE1ZIjdTA8BXL/d1NYmJCtkUTL9BL poA4HUko3jB/KIefuL+m158r9wXXFFWirUeEGzPR+Mw9vOW6gelMmOfHB7jRwXjlHN9q DZXoB5Eq/+rquYyw9Z6xRmvCrUZ+ErOdvSnSYhAkT6uuxbWVIrG/CyCw6mFJPgpUbfry Bz/X1ditAW175hnwh7L1yig20Q0Vr94CpBFlKVW6zyo35JzYdFqAs/Ng3AWyccjwj3cA 275Q== 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; bh=yL2rXiHBVpzxjWfWWjx4AyfIRFe/Oy4Y0W5dQ/8jozQ=; b=oC/f084dUzLFv1/RJvWBkm5i/g6bDnohLj9swpOb0+y9lLN3l1tr0tI1L4zGq8Yyw5 wbIsW/1gvkF+fW5ouPGxoaDqyKa0HCj7JfWmeHzsKxRHfyGFlWKg7cW7pxFW5qo4Nbxy /I/HEnRgFzbYVytABjsdWurGLRElQ7c5qQ7Yvj16QIhUh72WcwyZvwvYAfhrncimFqVP N8WjY0DzCp7x5jerDysugOKml0hCPZHbolR5fEmylXO7XzN16pkBzDi0JL1DKzl389kz OeksiNMjuFmMSvYq8j3+36noEKAUbHNHrtOpnVk5pt4DUbF0XPS4v/rwqhRCkjbRpuun GxYw== X-Gm-Message-State: AGi0PuYzf0w/8/VHtAHIClnc9AUTc7SXpTvnAsIA96bUn2KHwOlBDtYK adj3qaTG88f8Znm3arxAaZcSRB+sATCL+VlszR0rzObn X-Google-Smtp-Source: APiQypKXpcdE+L0iNsf/VR2mF1QIwcSBuG+2xmS0p1IQLNE7HWcpEXf5lag7IWSQEoFwDKdag2f9OxKZHDTp3EuybDY= X-Received: by 2002:a92:db04:: with SMTP id b4mr150088iln.120.1586444740276; Thu, 09 Apr 2020 08:05:40 -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> <000d01d60c4a$a213fb20$e63bf160$@gmx.de> In-Reply-To: Date: Thu, 9 Apr 2020 16:05:28 +0100 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="00000000000033378405a2dcf339" Subject: Re: [PHP-DEV] [VOTE] Userspace operator overloading From: rowan.collins@gmail.com (Rowan Tommins) --00000000000033378405a2dcf339 Content-Type: text/plain; charset="UTF-8" On Thu, 9 Apr 2020 at 13:18 (and subsequent correction), Dan Ackroyd < Danack@basereality.com> wrote: > > $a = new A; > > $b = new B; > > var_dump($b + $a); # calls B::__add($b, $a); OK > > var_dump($a + $b); # calls A::__add($a, $b), which is a TypeError > > > ... that code does have a TypeError. It is > calling '__add' and passing a parameter to the method that the code > can't handle. > > It appears to be the same error case as: > > ``` > class A { > public function add(A $rhs) {...} > } > > class B { > public function add(A|B $rhs) {...} > } > > $a = new A; > $b = new B; > > $b->add($a); // Ok > $a->add($b); // TypeError > ``` > As with so much else on this topic, it depends how you think about operator overloading - and I think that's why it's so hard to agree on an implementation. It seems that you're picturing the overloaded + as like a normal method but with special syntax, so that $a + $b means the same as $a->add($b) and only that. In that interpretation, it's perfectly reasonable to have the operation succeed or fail based only on the left-hand operand, because that's how we're used to method dispatch working. But if you look at how "normal" operators work, it's far less obvious that the order of operands should play any role in that decision. For instance, when mixing float and int, the result is a float if *either* of the operands is a float: var_dump(1 + 1); # int(2) var_dump(1 + 1.0); # float(2) var_dump(1.0 + 1); # float(2) var_dump(1.0 + 1.0); # float(2) Substitute 1 for $a and 1.0 for $b, and you're back to the example I originally wrote. Note that this is true even for non-commutative operators like exponentiation: var_dump(2 ** 3); # int(8) var_dump(2 ** 3.0); # float(8) var_dump(2.0 ** 3); # float(8) var_dump(2.0 ** 3.0); # float(8) My impression is what people consider "good" use of operator overloading is much closer to "make things act like built in numerics" than "make operators with fancy syntax", so some form of symmetry is necessary I think. Regards, -- Rowan Tommins [IMSoP] --00000000000033378405a2dcf339--