Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115688 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 15161 invoked from network); 11 Aug 2021 03:36:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Aug 2021 03:36:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C86C3180212 for ; Tue, 10 Aug 2021 21:06:38 -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,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-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) (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 ; Tue, 10 Aug 2021 21:06:38 -0700 (PDT) Received: by mail-lj1-f171.google.com with SMTP id y7so2330875ljp.3 for ; Tue, 10 Aug 2021 21:06:38 -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=0i1KC4sxwBKCO9CJRBOV9EoXcbQDRQnH6YCANX05680=; b=GW36kwXdrw4WtZsKFsBFyfuvEgkS2ZBEQEn5NsPlAFIXlU5NPncOniItj4LUrkXa5/ xk6lYM1MwOpxL8a+lAlxOQwSV7eLB7K1s/BfYfNCYbXprnqNgz41MFDYSOr1E7FxpJj8 iRREiPvaL72TYtlLNVebvs47SQzXQhF6XsBITkuzpXs08h4Jwk48s1VLHtk+8a8JARVb tO4HY5HPxfvUjeN075hvEoytMp3ApHNn3OOVYb/rYlpZOad4RR2zGEZO9vivSapFAnjO bk3ucnPeVq8ULzvRrRy9d6neaY0DBZL/G9C8pwmQU7yA08A70r/WYrCPeIIjpdvsl/Tf 6oTA== 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=0i1KC4sxwBKCO9CJRBOV9EoXcbQDRQnH6YCANX05680=; b=SbQMRz7FF9RBboFnmM9kPrsFsnrB3GH/2rORodADHpoYhJaWCo1tx72uHrqyPFs2Us 5EHNBWF5jq+h5z1b9AZ0lAN4X1l+qBOf2Hkp+yvUSkUWPEchBhfUxheUxRYT3RLb8DLu 3qrv2tu6bpgtlO0YJeOk/tKc7LnG7w5P7qyRltDwVmafpXNi7wRxnrXKszKMagXRXzmW tdW1+QPNCAqKvkPQklXy8z6lMRMOnijyv1RGXkZunu7NN8gHwrUJpZYjXjKKLlidCWAh ezVHnT3mGR449Bn9grBzGg4MnINLVbyLw4BricCqegjiQ8Dg9+Rk38zIk5xfm4hQddaW CmAw== X-Gm-Message-State: AOAM5316DVvztMBUsPTZA9Xy6krNvRh2Tc5RVOQBmTADyroY8U2ReT0d 55BrPAf90nWISm5xwfFqe+U+1Sja98M0gF/0PO4xsdM5aDqzyw== X-Google-Smtp-Source: ABdhPJzkSodHBvg2iEwS+cGWFIdmsrpy+8/rwP3IZ1Dr24g4tCvmbTOYm+b/KY4cAqQliEyr05hRnUnf9EOZsNKimhc= X-Received: by 2002:a2e:900c:: with SMTP id h12mr20805247ljg.240.1628654792721; Tue, 10 Aug 2021 21:06:32 -0700 (PDT) MIME-Version: 1.0 References: <94696d46-c4e6-406a-b859-89144bff31bf@www.fastmail.com> <3D7F74C1-F1A0-4258-8EE3-3C143B6B54EB@newclarity.net> In-Reply-To: <3D7F74C1-F1A0-4258-8EE3-3C143B6B54EB@newclarity.net> Date: Tue, 10 Aug 2021 21:06:31 -0700 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="00000000000061c6a205c940be23" Subject: Re: [PHP-DEV] Revisiting Userland Operator Overloads From: jordan.ledoux@gmail.com (Jordan LeDoux) --00000000000061c6a205c940be23 Content-Type: text/plain; charset="UTF-8" On Tue, Aug 10, 2021 at 8:46 PM Mike Schinkel wrote: > > Either way if PHP can identify a value object then it could limit operator > overloads to just classes that are value objects by whatever approach PHP > chooses to distinguish. > > > -Mike > [*] Ironically I mentioned you because I thought you would be appreciate > seeing another reason PHP could benefit from value objects. But as they > say, no good deeds go unpunished. > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > Here is a quick and dirty example of just the complex number use case. It assumes that the operator overloads already are built in. A couple of things of note: 1. The actual mathematical logic to cover all the possible input and return types is extensive. Though the execution time and complexity are quite limited, the code complexity is high due to the number of conditionals. 2. I chose to implement the various operator overload functions in different ways to illustrate different methods that might accomplish the same task in userland code. For instance, in some cases I typed against the SimpleNumber abstract, while in others I expanded SimpleNumber out into its concrete implementations, Real and Imaginary. 3. I only implemented the _add() and __mul() methods for this example, as the reality is that getting into some of the more complex operations would be so much code that it wouldn't be a digestible example. The __pow() method for ComplexNumber would be in excess of 100 lines and also require a polar coordinate representation within the class, for instance. This isn't a working implementation, in that this code will produce errors if run in PHP (due to the absence of operator overloads). Please note that if we were to go the route of creating domain-specific classes to cover this use case, the actual classes would be many, many times longer than this. This is some of the simplest logic involved in this particular use case, which is why I chose it as the example, and that perhaps illustrates why I am willing to explore such objects but do not believe they are the best choice at this time. https://github.com/JordanRL/operator-overloading-rfc/tree/master/use-cases/complex-numbers Jordan --00000000000061c6a205c940be23--