Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125605 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 390B81A00BD for ; Tue, 17 Sep 2024 22:13:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1726611322; bh=Cr7NKpdJAl96wlXxdL2bVC9/6B/nHWu2w7mpMMPEFic=; h=Date:Subject:To:References:From:In-Reply-To:From; b=MwUHxeOtyQM5oRM9FRFazcwLZ46oZRUNJCMRgWtU4/JlTcy6s+6RBtr+V9oJuMDfz YmFuh/DWrHSyeT/czTjY6v4SIv2G9NGUDvnF2Bsbugtf9yVnmothU+so1ktD534Qrw q+G0y0tCm1tZvvrkCPquCNYr9ZQXVdA03RPjL9S90hk6sOKq30pomz4x2OVnWDTix+ Q/oFk9kjQCLq+c2nyAaItUZNlol/d5aVZhYlfbrNVUDmoC52V0WluENypF/K+t70SC +ZExqJ165Xthj94YrpQHpseVS7sgSdZSnXvo1qQbY3+L6ZdfgD9EzC6SQVOtbt29a7 +vzUpCbGLNdlQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 34892180053 for ; Tue, 17 Sep 2024 22:15:21 +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,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh7-smtp.messagingengine.com (fhigh7-smtp.messagingengine.com [103.168.172.158]) (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 ; Tue, 17 Sep 2024 22:15:20 +0000 (UTC) Received: from phl-compute-06.internal (phl-compute-06.phl.internal [10.202.2.46]) by mailfhigh.phl.internal (Postfix) with ESMTP id 0A7111140107 for ; Tue, 17 Sep 2024 18:13:15 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-06.internal (MEProxy); Tue, 17 Sep 2024 18:13:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :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=1726611195; x=1726697595; bh=ysf+SsQPkw JdqUx21Ae6GUZfUePlaUldrOKPj9Mz4BI=; b=FsmzyQq5lwhVLj7NGSu02KsSim p81D9Fkst5n4uVzRqJGb0YmCM8T3rJ8V7+FIVYQIj7uA1/rH7pxwn2GN/MWn6rCp FlR9OyIp+JRbmrzQmA2YS7x1xl19FvlG04A14DW+VJwxOa5m7bkMvzh9YFLnKYTi LXPdA5csZos/bqwsgmqOJV0RzEZyYHaXmv3PDW5j+VHCqVWwBTXu8tjMTYzOcSUJ e3hwhe1ThDDXwJY72Pn5x9PAk2NSfLWyHxJBdW4bXiWMRBrjiOKo4PSWBMz8v11m soVMO8oRx0zQdhS5AyJkgfKerajMjemxNLfdhHZiW4xI4D1s4DHLJqfJoR2Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1726611195; x=1726697595; bh=ysf+SsQPkwJdqUx21Ae6GUZfUePl aUldrOKPj9Mz4BI=; b=E+Az18xGfKfUCqej2ym3JRvXtw+4Twg39w8ErRXOxeGR 5vMz2ZU7oY0BL7WfW7RmkNgeysq8E4NW5lk+dmPQIljhP8+f4IXhsBX5BFXFf1wv vZhNapv2AJGRjjUD4LKY9jtO2rHU+AfjVSmfnaGAHd1dcvM2i8xqmDX1mcd4N+8a ylMeO70dRjiV1JVYa/DsJyRiHlytzWQwih3cu6BUznrcthLvlXoIhaZRglvSjMyO yX1krBt4VcKvT1XFYvkMCAqPjmpz64dQKHv9RZDEYYCB8CcDTfYdAyYPQlqm3Wy1 hnoiyhOY5uRX7uW8i5Er8+q60OxAeGpaFMOJHGTqNg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudekkedgtdejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucenucfjughrpegtkfffgg gfuffvfhfhjgesrgdtreertddvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhs ucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecugg ftrfgrthhtvghrnhepheetleeiiefgueduieeuieffvdevheduueefkeejuefgffeftdei tdegtedtleetnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepihhmshhophdrphhhphesrhifvggtrdgtohdruhhkpdhnsggprhgtphhtthhopedu pdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsth hsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Tue, 17 Sep 2024 18:13:14 -0400 (EDT) Content-Type: multipart/alternative; boundary="------------hlfCB5s907kVvc1d3FV0FpiH" Message-ID: <12de3d03-3426-44f8-a83f-d686d2034912@rwec.co.uk> Date: Tue, 17 Sep 2024 23:13:13 +0100 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [Pre-RFC Discussion] User Defined Operator Overloads (again) To: internals@lists.php.net References: <2551c06a-ec1f-4870-a590-aeb5752fc944@rwec.co.uk> <0f0444eb-8fc5-4c56-8528-5aa528988e73@rwec.co.uk> Content-Language: en-GB In-Reply-To: From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") This is a multi-part message in MIME format. --------------hlfCB5s907kVvc1d3FV0FpiH Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 17/09/2024 21:37, Rob Landers wrote: > I would prefer to see something simple, and easy to reason about. We > can abuse some mathematical properties to result in something quite > simple: > > 1. If both are scalar, use existing logic. > 2. If one is scalar and the other is not, use existing logic. > 3. If one is scalar and the other overrides the operation, rearrange > the operation per its communitive rules so the object is on the > left. $scalar + $obj == $obj + $scalar; $scalar - $obj == -$obj + > $scalar, -($obj - $scalar). It is generally accepted (IIRC) that > when scalars are involved, we don’t need to be concerned with > non-abelion groups. > 4. If both are objects, use the one on the left. > Step 3 requires operators to be overloaded in groups: the rearrangement of the binary "-" operator requires definitions of both the unary "-" operator and the binary "+" operator; and definitions that meet the appropriate mathematical rules. IMO, that's a lot more complicated than calling the "+" overload with an OperandPosition::RightSide flag; or Python's approach of separate "add" and "reflected add" magic methods. Since you mentioned Scala, I looked it up, and it seems to be on the other end of the spectrum: operators are just methods, with no mathematical meaning or special dispatch behaviour. In fact, "a plus b" is just another way of writing "a.plus(b)", so "a + b" is just a way of writing "a.+(b)" Maybe it would be "useful enough" to just restrict to left-hand side: 1. If the left operand is an object which implements the specified operator, call that implementation with the right operand as argument 2. Else, proceed as current PHP. This is where gathering a good catalogue of use cases would come in handy: which of them would be impossible, or annoyingly difficult, with a more restrictive resolution method? Regards, -- Rowan Tommins [IMSoP] --------------hlfCB5s907kVvc1d3FV0FpiH Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 17/09/2024 21:37, Rob Landers wrote:
I would prefer to see something simple, and easy to reason about. We can abuse some mathematical properties to result in something quite simple:

  1. If both are scalar, use existing logic. 
  2. If one is scalar and the other is not, use existing logic. 
  3. If one is scalar and the other overrides the operation, rearrange the operation per its communitive rules so the object is on the left. $scalar + $obj == $obj + $scalar; $scalar - $obj == -$obj + $scalar, -($obj - $scalar). It is generally accepted (IIRC) that when scalars are involved, we don’t need to be concerned with non-abelion groups.
  4. If both are objects, use the one on the left.


Step 3 requires operators to be overloaded in groups: the rearrangement of the binary "-" operator requires definitions of both the unary "-" operator and the binary "+" operator; and definitions that meet the appropriate mathematical rules.

IMO, that's a lot more complicated than calling the "+" overload with an OperandPosition::RightSide flag; or Python's approach of separate "add" and "reflected add" magic methods.


Since you mentioned Scala, I looked it up, and it seems to be on the other end of the spectrum: operators are just methods, with no mathematical meaning or special dispatch behaviour. In fact, "a plus b" is just another way of writing "a.plus(b)", so "a + b" is just a way of writing "a.+(b)"


Maybe it would be "useful enough" to just restrict to left-hand side:

1. If the left operand is an object which implements the specified operator, call that implementation with the right operand as argument
2. Else, proceed as current PHP.


This is where gathering a good catalogue of use cases would come in handy: which of them would be impossible, or annoyingly difficult, with a more restrictive resolution method?

Regards,

-- 
Rowan Tommins
[IMSoP]
--------------hlfCB5s907kVvc1d3FV0FpiH--