Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115664 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 32331 invoked from network); 8 Aug 2021 09:38:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Aug 2021 09:38:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6632E1804C8 for ; Sun, 8 Aug 2021 03:08:15 -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-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) (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 ; Sun, 8 Aug 2021 03:08:14 -0700 (PDT) Received: by mail-io1-f43.google.com with SMTP id 188so20470158ioa.8 for ; Sun, 08 Aug 2021 03:08:14 -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=E7Mm/9dZlOkdyuZNhxrHbI3kI5WOjEvQosEjssGMXk4=; b=MRQd/cZitG9KapPljh1i0HzeygpBDn4POjdDOUecSJ63ZSJOcZfXYaZLKl4oWhyNya cJ7003WZysOim64gmFMg7olC4AlDCZ72iKCLNLfDWuc1AA8cnVhec1myJf9qFRuglqKs 4zOPjm7ia2UyiNZURqqCNe60sErlplvgybQFXmWCuhhtp3EmlyND3lU+zTvtTfXAQhQy FudgrRKIQb0EH42n9ly5Iw0mgf4eg6HALHGayVkPrVbk6A9KI7um/D68/CA6UjfSgysC fNHMMRujqudCcYH9Rxtjoo6RybDvr3n7XmAJrMomMDI4y6lUrqhAN8AlE3YNlvVZovy2 Q6Hg== 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=E7Mm/9dZlOkdyuZNhxrHbI3kI5WOjEvQosEjssGMXk4=; b=g0FU9FxVshRKXKETAZstYnQlQMQ06JCKm/Se14LSO/u4OsobKvMLrrc4/La07w4Y1S o74+K4VAoVfLZ/M762O+OiHjDJQRQE0w8xqurE5WqmwFuG1BP1/sw59NDfAbPHh2ttL8 XNMS8ENjMl8NmQIa4aal97sNKFRXkJKPP1qV43P2xZmn7A8kA2yd4rHAq+iCsOXi1HnA lxguLTUOG/c6zYvlZLjFJa7kSMV6+ntx/69zMinQLgmoBT+Nm+cF1JTZ4qvl0ldimX0F 8VRGr10reidMScXFiSSAqU/xp0LMdJ2We0qh1B7y2tqIn92EirN32hrvfA90cWYxbCLZ FIxQ== X-Gm-Message-State: AOAM530RAfkpYGuYa/WhfK/aZX9WbcundLvnW0LFg5RYk84yjcHOB4iU xloBu4Q/Y5Um4l1+cdwXYGkebXwBD6fnwfC12rg= X-Google-Smtp-Source: ABdhPJznbIV0LdMUOx8YnvsPhMm3/AkL0guLfQp/kRuvXFRc/tkb6wJJjd8dMblLDUQUjq3PRKxeR2uCv2uhTCbm9aU= X-Received: by 2002:a92:194b:: with SMTP id e11mr384485ilm.200.1628417293063; Sun, 08 Aug 2021 03:08:13 -0700 (PDT) MIME-Version: 1.0 References: <94696d46-c4e6-406a-b859-89144bff31bf@www.fastmail.com> In-Reply-To: Date: Sun, 8 Aug 2021 12:07:58 +0200 Message-ID: To: Jordan LeDoux , PHP internals Content-Type: multipart/alternative; boundary="0000000000004c9bb505c9097243" Subject: Re: [PHP-DEV] Revisiting Userland Operator Overloads From: deleugyn@gmail.com (Deleu) --0000000000004c9bb505c9097243 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I started reading this thread with a total bias against it and as I read through it I can only imagine how painful and annoying it is to maintain a math library in PHP. Operator Overloading is truly a marvelous piece of functionality for that domain. However my biased still exists: it would be awful to have to understand what `+` is doing for every file of every library or even different objects in the same project. > > Ruby has operator overloading, and one of the distasteful aspects I fou= nd > > regarding in programming in Ruby was that everybody's Ruby code you > looked > > at felt like I was written in a slightly different language. I would ha= te > > that to become the case with PHP, more than it already is. > +1 > > 2.) Would it possible that instead of adding operator overloading we > could > > add classes that could be extended where PHP actually defines the > operators > > for those classes? > +1. > In any case, it is certainly possible that we could instead implement som= e > magic *objects* which can be extended that have built-in overloading in > specific ways. I think this is actually worse for the following reasons: > > 1. It would be far less obvious to the programmer that an object would > behave differently with a given operator, because the behavior wouldn't b= e > documented in the code itself. > For my vision, this point doesn't make sense. After reading my reply if this still holds true please extend upon it if possible. 3. It would likely result in some maintainability concerns as more users > demanded different DSL type implementations be included in core, and the > existing ones would need to be maintained. > True, but I see this as a good thing. The bar to bring something into core is so high that this is a protective layer against overdoing it. > As I've mentioned, while I understand that to many programmers > commutativity is a requirement of a good language, it is explicitly > incorrect to require commutativity for math operations. The arithmetic > operators are commutative for the real numbers, but there are many valid > math concepts for which they are not commutative, and I feel like pushing > for commutativity is a mistake. It is an attempt to force a behavior that > we want on code that it may be incorrect for. > Totally with you here. It's been nearly a decade since I part ways with Math, but if I'm not mistaken we can tackle most (all?) of the operator overloading with 2 classes: Number and NumberCollection (for lack of genetics). Everything in math can programmatically be represented as these two *abstract* classes. Number: 0 1 27 -30 14 + i f(x) 7/0 (joking!) 10! 15/8 3.141592... 0.9999999999999... 45=C2=B0 |-10| log(n) NumberCollection Matrices Sets Expression We have precedence for this: Datetime objects provided by PHP have operator overloading. However it might be best for PHP to steer away from the implementation and provide the abstract classes so that userland implements them. These classes would allow operator overloading and if possible maybe even a small hint of method overloading? ``` public function __add(Number $number): Number|NumberCollection { } public function __add(NumberCollection $number): Number|NumberCollection { } ``` I confess I may be oversimplifying it too much, but I thought it was worth asking. This could still be abused for crazy stuff, but by using inheritance we make a best effort to delimit the scope into a mathematical context. My opinion is that operator overloading is bad for the language and is a must-have feature for mathematical context. This seem to be the closest to a common ground I can find. --0000000000004c9bb505c9097243--