Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115657 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 86668 invoked from network); 7 Aug 2021 19:37:37 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Aug 2021 19:37:37 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2A5D0180539 for ; Sat, 7 Aug 2021 13:07:24 -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-lf1-f45.google.com (mail-lf1-f45.google.com [209.85.167.45]) (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 ; Sat, 7 Aug 2021 13:07:23 -0700 (PDT) Received: by mail-lf1-f45.google.com with SMTP id c24so22020952lfi.11 for ; Sat, 07 Aug 2021 13:07:23 -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 :cc; bh=5+543FMF2aMjB9sh6TlTqsv7irtR06OymP+5nJCI6zU=; b=a9g7S5ypYqvcL7dRSqcfjsxJaBqElqVRPlN+ineq9mTzfMpiFtHUwkZhheVgpnIX4J Vm9TL9pW/flLxzOoKwfox1Wul9nZpbTFId30VNUJJK88dGgs33F8ACayLSgbVCdTUePb 5/lytNMd/pge8MaJsBvdqpvMyFRACJ+YAGMjNsSMbU+fd0nzoG++rN8icOEZyQI7+RL+ ARr/tCqNCswbmwKRkeWFLn74te4QTmnhGSRfm5V9ZtkXZdBXK5mvL7R1HR+1IPrZQDuD UMS2Vf24oqwZuIBBpfmvragNK7etE8kpAKuUrxZs0cc5aGEt5KdKwjOo15Jj6L3p1Hh6 Tz4w== 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:cc; bh=5+543FMF2aMjB9sh6TlTqsv7irtR06OymP+5nJCI6zU=; b=rt3/DIkecpaVWFC+GBoxDTmKnLkk/sYUqp7v7HMangpf/aUEsJMHt0uQfXGOWTEV8U 8sTb6QCrsQzZNmzSGUUDgPQb4HsbIfygKDB+9+v6ZquU9f2JGQBVBrxI7n9HH7G5tukD pXq8IcAnMS6vpEEzBBWJHtwfTwvzw8wLLrnOaJ2bL2E9WX5AYsQuHECaaBj0EAaUi6ce kgopCDKehE+RFnw+3Jqzu93ys2ymTSin7Z4KotSXV1v4TOjaz9mHjH0Ky7f72WoKKnTW 4jqyYAOMHPkA7jQz0792pb6ObqZ3rIsrYYMIRnDRil4/j/sbCBV/S9O7sxDVC9dGb18c 9PKg== X-Gm-Message-State: AOAM5307LXHwgedObrMJ3VwvwI5AyLtyo4Zn+vawy74UoAgnypezaoI5 ZQE2B0F9baHcprtlh2MQyeOxdrbbFTPm1Ioghuo= X-Google-Smtp-Source: ABdhPJxIOgBI+d9ovIaZx2NhIRc0MPQCF+rZN51GpGnsk0zXMF7+cqbpUImemqE3y+RUZMgNmAljjd/c1gS08eSIJJk= X-Received: by 2002:ac2:5471:: with SMTP id e17mr11433477lfn.216.1628366841435; Sat, 07 Aug 2021 13:07:21 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 7 Aug 2021 13:07:24 -0700 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000025d96705c8fdb3e7" Subject: Re: [PHP-DEV] Revisiting Userland Operator Overloads From: jordan.ledoux@gmail.com (Jordan LeDoux) --00000000000025d96705c8fdb3e7 Content-Type: text/plain; charset="UTF-8" > a) Treating operators as arbitrary symbols, which can be assigned any operation which makes sense in a particular domain. > b) Treating operators as having a fixed meaning, and allowing custom types to implement them with that meaning. I think this is the core design choice that will affect how the implementation is approached, and having some good discussion around it before I got into the implementation was the goal of this thread. :) Jan's proposal for 8.0 fell more into the a) category with each symbol being given an independent, unrelated, and unopinionated override. That RFC very nearly passed, the vote was 38 for and 28 against. My one hesitation in pushing for a b) type implementation right now (which I favor slightly personally) is that the basic math operators do have very different meanings between arithmetic, matrix/vector math, and complex numbers, all of which are in the same domain of "math". Granted, only objects which represent a number valid for arithmetic could also be used with other math functions in PHP (such as the sqrt() or cos() functions). However, they are definitely use cases that are well treaded in userspace code and libraries. Complex numbers, for example, couldn't implement a __compare() function at all, as they don't have any consistent and sensical definition of "greater than" or "less than". This means that if an object represented a complex number, the following code would be perhaps unexpected to some: if (10 < $complex) { // Never gets here } if (10 > $complex) { // Never gets here } if (10 == $complex) { // Never gets here (!!) } $comparison = 10 <=> $complex; // Nonsensical, should throw an exception So while I tend to lean more towards a b) type implementation myself, even within that I understood there to be some non-trivial considerations. "Numbers" in PHP are obviously real numbers, instead of matrices or complex, so all previous semantics of operators and math functions would reflect that. To me, an ideal implementation of operator overloading would be both: 1. Flexible about the contextual meaning of a given operator. 2. Somewhat opinionated about the semantical meaning of an operator. This is obviously challenging to accomplish, which is why I'm leaving myself nearly a whole year for discussion and implementation. I don't want to do this quickly and end up with something that gets accepted because we want some form of operator overloading, or something that gets rejected again despite putting in a great deal of work. Jordan On Sat, Aug 7, 2021 at 12:07 PM Rowan Tommins wrote: > On 06/08/2021 17:34, Jordan LeDoux wrote: > > 1. The only supported operators in the RFC will be the mathematical > > operators: (+, -, /, *, **, %) > > 2. The RFC would also provide an interface, (something like > > MathObjectInterface), that has all the magic methods in it. > > 3. The do_operation would be changed to check for the interface being > > implemented instead of the specific method. > > > Hi Jordan, > > In a previous thread [1], I wrote about two fundamental design > approaches to operator overloading: > > a) Treating operators as arbitrary symbols, which can be assigned any > operation which makes sense in a particular domain. > b) Treating operators as having a fixed meaning, and allowing custom > types to implement them with that meaning. > > Both approaches have their pros and cons, but my general impression is > that approach (b) has more support on this list. > > For approach (b), I think your proposal to group operations into > interfaces is a good one - it doesn't prevent someone using the > operators to mean something completely different, but it expresses a > clear opinion. > > I think using "+" for a union of two collections is really an example of > approach (a) - it doesn't really mean "add", and it's no less arbitrary > than using "<<" to add an item to a collection (as is conventional in > Ruby, for instance). If we want to go down that route, and allow > individual operators to be overloaded, I think we should look for a > syntax that lets you pick the symbol, rather than giving them names > which only make sense in some contexts. > > > [1] https://news-web.php.net/php.internals/108347 > > -- > Rowan Tommins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --00000000000025d96705c8fdb3e7--