Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125612 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 6584A1A00BD for ; Wed, 18 Sep 2024 01:48:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1726624216; bh=bLmRd3oT7MfFrCvn6R9rrAf3qy1OyLdSvwD8C6idCok=; h=Date:From:To:In-Reply-To:References:Subject:From; b=kGiG8XGmw3x0yuCXpRUkyZbIAnHF4MlStvCgN2nZHY+FJ3n+D99MtEZxOWUTUAU/B F4z7CdD5744nht1chQu8wJve2ZRl5c4UEcx7OcBPf0QPUI3CFIkFcowD3UlYF5XU6L BdqfOq6LD5NPbnkybeDruO885h7nHFbFpWxZpuWJGrLmH+hiBDawR23mYsWuvQOqLa SPvQ394w7mVNFq93KLNFeL0dMHu9JsepBVcdoF7MaH8HLFT1YS8VSCVsvlLz5GAB3t OvX0ptai4+t5K7iLewcKKDXVaVdM3b5Sg2bIZ9jnb8rU1MOV2SdT74CMnmKPY5siQY 0xU2G/+qMzsAA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 86135180068 for ; Wed, 18 Sep 2024 01:50:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout5-smtp.messagingengine.com (fout5-smtp.messagingengine.com [103.168.172.148]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 18 Sep 2024 01:50:15 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id 5D21B138029E for ; Tue, 17 Sep 2024 21:48:09 -0400 (EDT) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-01.internal (MEProxy); Tue, 17 Sep 2024 21:48:09 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm1; t=1726624089; x=1726710489; bh=2q5W7e1nny4xt/7TsRRg4 fV9nYcydz3+sGo5TrTIRD8=; b=eqHCcMMB1I7g6f9Xt932e9GCr0+y7gxVEr3kH 5UAutR2TZOeLH3t+9GLUVZw4C6qYjAlUecdK2kupBTBOsb3IuiwamLmrYGzuXn4j Hu0QfsLQIDjr5tCOuhHI6FcBxnacge6NfvqMRIqokRj9vOlsU34Zk3VRgBzj8TCu fsN9Fq3aMK0jTc+Q53+RuJ+JL4TuJVxR7Uf4HWR/nXfoVdaDZwwEr8kzNscT1/7J eV09ZAlw+565xiyiLCb6ilE+UV0MJ36yiYjq0VFqe1TIwFzo/NqGKCGiwBuydMeC mBcr0cYAEdrJzporok/nw9IxTjVo1Sg0/PPX2k9YW3EFAI3XQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1726624089; x= 1726710489; bh=2q5W7e1nny4xt/7TsRRg4fV9nYcydz3+sGo5TrTIRD8=; b=c JO9kqsE6It5slI3tcm0Vym61Fe8E7a4jyT0DesYKURzR5iSaGBqXB5zjFTiZ4iXp jNx+UGk4dFSWzVpVTMXqmGoDdFf3BdzVbLRHj3dlzL7RAZkWeem+Vwef3yDxJvWw dgk/DocMCgYlmG8p8FJVLaDCsKyIc3FPmF6SwQB3HpkIYs+I29FM6NFjihWfkFlA xM4ks19VmbR2xsELHeJpJMGm3ox72xn34NCKn07nJIIJuXQHDBshMHI2nFuHFkJ+ yVwKN2oSiWGzxpsb4kLMATECYBPQbYtrsu6TOpUjiCnIgA+PMB4HW9ywWriZvze+ fSGYEMD7pHqNGGpyqzpRg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudekkedgheduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthejredtredttden ucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfih gvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpeeuvedvudfhffffhfelueeh vdejvefgleegteegffetudefleehgeefvdehgeelteenucffohhmrghinhepphhhphdrnh gvthenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehl rghrrhihsehgrghrfhhivghlughtvggthhdrtghomhdpnhgspghrtghpthhtohepuddpmh houggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdr phhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 0DACD29C006F; Tue, 17 Sep 2024 21:48:09 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Tue, 17 Sep 2024 20:46:23 -0500 To: "php internals" Message-ID: <782bc7c2-f179-4a79-a651-b1979e594d08@app.fastmail.com> In-Reply-To: References: <2551c06a-ec1f-4870-a590-aeb5752fc944@rwec.co.uk> <0f0444eb-8fc5-4c56-8528-5aa528988e73@rwec.co.uk> Subject: Re: [PHP-DEV] [Pre-RFC Discussion] User Defined Operator Overloads (again) Content-Type: text/plain Content-Transfer-Encoding: 7bit From: larry@garfieldtech.com ("Larry Garfield") On Tue, Sep 17, 2024, at 3:14 PM, Jordan LeDoux wrote: >> I think it's absolutely possible - and desirable - to choose a philosophical position on that spectrum, and use it to drive design decisions. The choice of "__add" vs "operator+" is one such decision. >> > > Ah, I see. I suppose I never really entertained an idea like this > because in my mind it can't even handle non-trivial math, let alone the > sorts of things that people might want to use overloads for. Once you > get past arithmetic with real numbers into almost any other kind of > math, which operators are meaningful, and what they mean exactly, > begins to depend a lot on context. This is why I felt like even if we > were limiting the use cases to math projects, things like commutativity > should not necessarily be enforced. > > The line `$a + $b` and `$b + $a` are SUPPOSED to give different results > for certain types of math objects, for instance. The line `$a - $b` and > `$b - $a` more obviously give different results to most people, because > subtraction is not commutative even for real numbers. > > My personal opinion is that the RFC should not assume the overloads are > used in a particular domain (like real number arithmetic), and thus > should not attempt to enforce these kinds of behaviors. But, opinions > like this are actually what I was hoping to receive from this thread. > This could be the way forward that voters are more interested in, even > if it wouldn't be my own first preference as it will be highly limiting > to the applicable domains. I'm not sure where exactly in this thread to put this, so I'm putting it here... Rowan makes an interesting point regarding operators vs operations. In particular, the way the <=> logic is defined, it is defining an operation: comparison. Using it for anything other than ordering comparison is simply not viable, nor useful. It's defining a custom implementation if a specific pre-existing action. For all the other operators, the logic seems to be defined for an operator, the behavior of which is "whatever makes sense in your use case, idk." That is, to use Rowan's distinction, a philosophically different approach. Not a bad one, necessarily. In fact, I think it's a very good one. But, as they are different, perhaps that suggests that comparison should instead not be implemented as an operator overload per se, but as a named magic method. The existing logic for it is, I think, fine, but it's a fair criticism that you're not defining "what happens for a method-ish named <=>", you're defining "how do objects compare." So I think it would make sense to replace the <=> override with a `__compare(mixed $other): int`, which any class could implement to opt-in to ordering comparisons, and thus work with <, >, ==, <=>, etc. (And, importantly, still keep the "specify the type(s) you want to be able to compare against" logic, already defined.) A similar argument could probably be made for ==, though I've not fully thought through if I agree or not. Again, I think the previously defined logic is fine. It would be just changing the spelling from `operator ==(mixed $other): bool` to `public function __equals(mixed $other): bool`. But that again better communicates that it is a core language behavior that is being overridden, rather than an arbitrarily defined symbol-function-thing with domain-specific meaning. There was an RFC for a Comparable interface back in the stone age (2010), but it looks like it never went to a vote: https://wiki.php.net/rfc/comparable Arguably, this would then make more sense as a stand-alone RFC that happens to reuse a lot of the existing code and logic defined for operator overloads, which are all still just as valid. That does not apply to the arithmetic, bitwise, or logic operators. Overriding + or / for a specific domain is not the same, as you're not hooking into engine behavior the way <=> or == are. For those, I'd prefer to stick to the current/previous implementation, with the `operator` keyword, for reasons I explained before. Jordan, does that distinction make sense to you? --Larry Garfield