Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116667 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 42842 invoked from network); 17 Dec 2021 05:20:34 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Dec 2021 05:20:34 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 70B0B1804AA for ; Thu, 16 Dec 2021 22:23:12 -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.2 required=5.0 tests=BAYES_20,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-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (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, 16 Dec 2021 22:23:12 -0800 (PST) Received: by mail-lj1-f176.google.com with SMTP id 207so1687882ljf.10 for ; Thu, 16 Dec 2021 22:23:12 -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; bh=+VN723HioDlRLHW+nRgRhCekESLbN/+863NZt/8O0Ds=; b=kx30QmB3Dfy/IMHs20MdJpDUif43NBQX76I5LWIR7miZqMgAoey0jORYKb/AdXkhNz qfIQak1oGp/aXhLJGkKzDuwvq+/0X+fbfkdEz/jj9L69w8ayIpKHjnsIv15VUUpU6Ohf iqnsUi7jq2VRZr9cwCqQeQHn8wuS9J/98QVW8aeNz5Vog6r47b61XZkPhVusDozi9I8t pGdUUtYrt8YfCz7OrvN15fONgTKYfiQ+G0IJDDHwZv1WBNnPv15sqsISqHlBlp+0Rw3t ck00CKqeSCzYMx3ylEdwxtMUCeKlTMvm3W6J54VlkSZgO5b3Ik+t8w4zCEDNErqpJSm8 L1zQ== 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; bh=+VN723HioDlRLHW+nRgRhCekESLbN/+863NZt/8O0Ds=; b=PA9n1DvY47+xMMJeR3sBc0I0ia+xI/RzkqzKbb+cbiEFV04Qst3ogsq5QHjCr8jYZJ 3swXeGs7dclAWI3zIBIxPqDlEcXNI9MOpQZ7AJatv8I3l2ETEgfDN/TZX7lA1563o0Ke OF1umH5+ljdIn6rdznQfcv5GqKrN/dNFiRGqnCl9qXrXoPUydbBsP67AVguDLXYk5DBj ffzPayLY0VFHSHE+RmsEseLeFXb5545SVGqeEl2XtiA0gfyiMZce0BVUM/nfsowiOyl/ bJ6TX4fwbU/Qx3sFB0y5RKtDxFxKC3uQsBSFwhUE1cKZttANKc2CFvc7Eij1my9DsNMH MSkQ== X-Gm-Message-State: AOAM531Nz4TGaKzFTVTcVWw2o8QOG95k2j9G7K8QCwOrh/arh9vCBQOT ++4abeQqRMJDPE6AuZseQ+9eGErLf8HOlPGlgYvGjygf X-Google-Smtp-Source: ABdhPJyLGc4lFdCp2nY46DOM5LsCknLHqDH7nIFlVE5ItrSowTGSzGeHTmXfmh3HbNunIFQy5JZON9Pu7MIRV/1hpgM= X-Received: by 2002:a05:651c:549:: with SMTP id q9mr1518024ljp.25.1639722189892; Thu, 16 Dec 2021 22:23:09 -0800 (PST) MIME-Version: 1.0 References: <44b3fb4b-4693-1639-c8c0-5e17296c196e@gmail.com> <4b58c011-ed87-ba87-201d-0cf8e4116c6f@processus.org> In-Reply-To: Date: Thu, 16 Dec 2021 22:22:57 -0800 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000a8b3f105d351924b" Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads (v0.6) From: jordan.ledoux@gmail.com (Jordan LeDoux) --000000000000a8b3f105d351924b Content-Type: text/plain; charset="UTF-8" On Thu, Dec 16, 2021 at 4:21 AM Andreas Hennings wrote: > > Having one side of the operator "own" the implementation feels wrong, > and could lead to problems with inheritance down the line. > > From C++ I remember that at the time, there was a philosophy of > defining and implementing these kinds of operations outside of the > objects that hold the data. > > This makes sense in C++ because they are all statically typed and compiled, but the types of many things in PHP are not necessarily known until runtime, so it's a bit different. First, there is no situation in which the overloads will ever be called outside the context of an object instance. Any situation that would trigger a lookup for overloads will involve an object instance that has a value of some kind. `$val = $obj + 5;` They are not static methods because they *aren't* static methods in the engine execution context. Making them static would break scope needlessly in direct calls (if those are ever made), and would be a lie within the engine. Python is a language that handles them exactly in this manner. Under future scope I have a section titled Polymorphic Handler Resolution that addresses the issue of inheritance. I decided to make that a separate future scope for two reasons: 1. It has some unique efficiency and optimization concerns around resolving the inheritance structure during an operator opline. In general developers expect operators to be very, very fast operations. Overloaded operators will always be slower, but adding inheritance checks would exacerbate that. I wanted to consider that separately so that it could receive its own focus. 2. For those who have limited personal experience with operator overloads in the context of object instances (basically, those who haven't used Python or something similar), the reason for that requirement might be difficult to see. It will be easier to demonstrate the need to voters on this list after this RFC is implemented I think. There's already a lot of domain-specific background to this. The + operator *isn't* commutative in PHP right now, for instance. If you want to see how, consider `array + array`. Whether an operator is commutative is *always* domain dependent. The main edge-case that will be addressed by this future scope is: `$val = $parent + $child;` Where `$child` is a descendent of `$parent`, and both implement *different* overloads. In such a case, you want to execute the overload of the child class. An example of this would be a `Number` class and a `Fraction` class that extends `Number`. You would want to execute the overload on `Fraction` regardless of whether it was the left or right operand. This is how the instanced overloads behave in Python as well. I plan on bringing that as a follow up RFC before 8.2 feature freeze if this RFC passes. -- As a note, I have been swayed by the comments of many others at this point to make operator implementations callable directly on objects, and remove the callable restriction on them. The RFC has been updated to reflect this, and I will update the PR for it as well when I have the time. Jordan --000000000000a8b3f105d351924b--