Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116621 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72142 invoked from network); 11 Dec 2021 16:44:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Dec 2021 16:44:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1C9361804AA for ; Sat, 11 Dec 2021 09:45: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=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 11 Dec 2021 09:45:50 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id A46315C00AC for ; Sat, 11 Dec 2021 12:09:28 -0500 (EST) Received: from imap43 ([10.202.2.93]) by compute6.internal (MEProxy); Sat, 11 Dec 2021 12:09:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=m6ie0k 20mNOuUD8A1owi9YXtTL4kjR8f0P/7IwtXNX4=; b=JdK7f0I83c+tWHqM5upJxE 5faKeF8db1ZpFRIw/ZY96ubeLinqQVeQspGTxMrSbgw+/wSy+29BAKkpUSxpWbKn z1TDldD9bxbWg3vwCKBK812Xw/wRU1nn/jCiyOHMZ5Om+jjVPC6dTp5aR4LvbKvt pzbZ0wj+Zu5xTtrm2NukHlx5GJ/0NutXbGLW/pKlkbN3hLLxUavoZrZ2jz9nU6XR VVR3/p1l657Z75j4ktI4Vc+6+QzO1SNQ5Hxm9ovtpO0HqHoo9i5+H4W9A41TDwX4 92CPfSzL0LmrMDml1xq1M1KAau9KWvJAk8Iy+twKXAqiRGsdIHHAHE9MTo9MQuJg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvuddrkeeggdelfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgrrhhr hicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh eqnecuggftrfgrthhtvghrnhepgeelgfekudeivddvteffueejffdthfejieevhefgffek udevkedtvdelvddvffefnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrg hilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 73B28AC03DB; Sat, 11 Dec 2021 12:09:23 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-4514-g2bdc19e04f-fm-20211209.002-g2bdc19e0 Mime-Version: 1.0 Message-ID: <7126a5cb-fdaf-4e50-b8af-7d95965d1125@www.fastmail.com> In-Reply-To: References: Date: Sat, 11 Dec 2021 11:09:03 -0600 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads (v0.6) From: larry@garfieldtech.com ("Larry Garfield") On Sat, Dec 11, 2021, at 9:15 AM, Dan Ackroyd wrote: > Hi Jordan, > > On Thu, 9 Dec 2021 at 20:12, Jordan LeDoux wrote: >> >> Hello internals, >> >> I last brought this RFC up for discussion in August, and there was >> certainly interesting discussion. Since then there have been many >> improvements, and I'd like to re-open discussion on this RFC. > > In general I'm in favour of this RFC; a few months ago I was > programming something and operator overloads would have been a good > solution, but then I remembered I was using PHP, and they haven't been > made possible yet. I too far prefer this RFC to its predecessors, and hope it passes in some form. > However.....I think the new 'operator' keyword is probably not the way > to go. Although it's cute, it has some significant downsides. > > There are quite a few downstream costs for making a new type of > methods in classes. All projects that analyze code (rector, > codesniffer, PHPStorm, PhpStan/Psalm, PHPUnit's code coverage > annotations etc) would have to add a non-trivial amount of code to not > bork when reading the new syntax. Requiring more code to be added and > maintained in PHP's builtin Reflection extension is also a cost. > That's quite a bit of work for a feature that has relatively rare > use-cases. > > I just don't agree/understand with some of the reasoning in the RFC of > why using symbols is preferable. > > "In such a situation, using magic methods would not be desired, as any > combination of symbols may be used for the new infix. The restrictions > on function names, such as needing to reserve the & to mark a function > as being by-reference, would place limitations on such future scope." > > I don't get this. The magic methods in previous drafts of the RFC > don't have a problem with & as the methods are named with 'two > underscores' + name e.g. __bitwiseAnd. That does't appear to cause a > problem with an ampersand? > > "By representing the implementations by the symbols themselves, this > RFC avoids forcing implementations to be mislabeled with words or > names which do not match the semantic meaning of that symbol in the > program context. > > The name of the function (e.g. __add) always refers to the symbol used > where it is used, not what it is doing. > > If the code is `$a + $b` then that is an addition operator, when the > code is read. If I was reading the code, and I saw that either $a or > $b were objects, I would know to go looking for an __add magic method. > > " '// This function unions, it does not add'" > > Then that is probably an example of an inappropriate use of operator > overloads, and so shouldn't be used as a justification for a syntax > choice. I believe the intent is for things like dot product. int * int This is known as "multiplication", or we could abbreviate it __mul if we felt like it. vector * vector This is known as a "Dot product", or "scalar product", and is not really the same as multiplication. It uses effectively the same operator sigil, though. There's also cross-product, which nominally uses `x` as a symbol in mathematics. Which is... often also translated to * in code, but is a very different operation and is not multiplication as we know it, nor is it the same as dot product. So __mul() would be an incorrect name for either one. Using symbols, however, would (with some future extension to make it extensible) allow for: operator *(Vector $v, $left) { ... } operator x(Vector $v, $left) { ... } which (for someone who knows vector math) is a lot more self-explanatory than "which one is being mistranslated as multiply?" At least, that's my understanding of the argument for it. The point about making meta programming more difficult is certainly valid, though. Personally I think I could go either way on this one, as I see valid arguments either direction. > # "Non-Callable - Operand implementations cannot be called on an > instance of an object the way normal methods can." > > I think this is just wrong, and makes the RFC unacceptable to me. > > Although most of the code I write is code that just performs > operations as I see fit, some of the time the operations need to be > driven by user data. Even something simple like a > calculator-as-a-service would need to call the operations dynamically > from user provided data. I largely agree here. I don't know if it's because of the `operator` choice or not, but being able to call an operator dynamically is important in many use cases. It doesn't have to be a pristine syntax, but some way to do that dynamically (without having a big match statement everywhere you need to) would be very welcome. Another question: Can an interface or abstract class require an operator to be implemented? That's not currently discussed at all. (I would expect the answer to be Yes.) --Larry Garfield