Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115768 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11983 invoked from network); 21 Aug 2021 03:01:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Aug 2021 03:01:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 86B2F1804CF for ; Fri, 20 Aug 2021 20:35:00 -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-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) (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 ; Fri, 20 Aug 2021 20:35:00 -0700 (PDT) Received: by mail-lj1-f170.google.com with SMTP id q21so20366476ljj.6 for ; Fri, 20 Aug 2021 20:35:00 -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=9+8Dgt0wRZaLTMPm+C/pzfiIbyBkA9mPg0D6elCYYLE=; b=XjL/Hp1paUl/aOE8YgR4ak+ThAjIzpX7c5iDoPpBc/Ckc1ye4r00Wuu58LLWYVKiL0 vI3PbSr27a7GXM0yA5QewO0ljWS092vCsKdCRt8NPjuqcPNVdlXNQJ2GPFTVwhUn+mGX ApQUdCBwWVpqPougFmRU14s38HriahA5E0Ai9D6CkYCWpzB9d2K3nGQwAyxynmSIydN0 0rZabqmbumIGbHeLOGY4BMGDnzjqK3xXcgWtJo/s66QYIjb6j++byy39rpiyIFuIh0go aSrHsetDBRi+AWwV+Yq2U0xSihvIq0bMfa2iyNxuiS2kYdcspA4NhWrAPox+5YYkNkEP y85g== 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=9+8Dgt0wRZaLTMPm+C/pzfiIbyBkA9mPg0D6elCYYLE=; b=qxVaBS2ccEkcXyd3rMidXc4YVtJUiXDcwu6eyzHN1vPCsMhQsgANaMO/jOcpiJR3wd bQP6pIGcCodGLKxsuQyoBsbQfQq5inaIgMS5oqlvQkoEB6tZsdmJfkvrfIn6IMFdcjIn laeaFzl+jd9LAGC7ZsTDkJPbx4UG5ruEaOg/SRRyY9tsLXv+r+wNayv4PypPqoJx+Pjk 3q8lJIyZBuxhqqZjuzyBOEaMUn2omWHDadgT26qbJsTkojiVIaTvz9x5JqfLHOztlBu4 jtgNZGcsCFqYU4w6/X0FL8mc0njdX+GjPcUa/4JWL5Xxj7qva8o61ePKH7dmAfNEvlHd sMhQ== X-Gm-Message-State: AOAM531udDPvm8iIr0BBQbnoFDvk7dKX+aV6EX79mXnCaIhY3zxP3O/O FnpFNb76g3Ds+8arLfA14VExLQy7T+phowsvh7lnoQZD5zvk9w== X-Google-Smtp-Source: ABdhPJzs8DU9lt+FQ6QnzNYH5p0bFkbON1AYoqSSpkvRTwF/DTrymDqx98s7tEgtf/TJsNmq6hLQGonsFv2yeN6TfEE= X-Received: by 2002:a2e:9010:: with SMTP id h16mr18205828ljg.484.1629516897515; Fri, 20 Aug 2021 20:34:57 -0700 (PDT) MIME-Version: 1.0 References: <2fd2d665-ca3a-4fa8-9196-6c8b1b2a3da0@www.fastmail.com> In-Reply-To: <2fd2d665-ca3a-4fa8-9196-6c8b1b2a3da0@www.fastmail.com> Date: Fri, 20 Aug 2021 20:34:46 -0700 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000d4fe4f05ca097777" Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads From: jordan.ledoux@gmail.com (Jordan LeDoux) --000000000000d4fe4f05ca097777 Content-Type: text/plain; charset="UTF-8" On Fri, Aug 20, 2021 at 9:49 AM Larry Garfield wrote: > > - The "Separate codebases" risk is odd. As you note, "the same as > anything else, duh, copy-pasta will bite you." I'm not sure why that's > even listed. > > - The larger risk would be competing implementations that use operators > differently. Eg, I could see one collection implementation using / to > indicate "remove these elements" (because that's the complement of modulo, > which is "all elements except these"), and another to indicate "give me an > array of these many equally-sized collection objects." That would be > confusing if you bounce between two implementations on different projects. > (Eg, Laravel and Symfony.) (We could argue that one of those > implementations of / is dumb, but that doesn't mean they won't exist.) > > Agreed. I reworded and simplified that subsection. > - I don't really grok how $queue -= 1 would work. If that expands to > $queue = $queue - 1, then the = operator would return the value assigned > ($queue, after the value is removed), and you would get the next item... > how? I don't know how you'd do that, making that syntax nominally valid, > but useless. > I added more details to clarify what the example was actually about. It was *intended* to show how such usage is ambiguous and incorrect. Because a class that implements __sub() can't *prevent* it from also working with the reassignment operator -=, usage in this fashion will be either highly discouraged or loudly communicated by the library, which in either case should help promote good community standards around the feature. In this example, the __sub() method didn't return an instance of Queue, it was returning something like an array of `int` items from the queue, which obviously behaves terribly with the reassignment operator. > - "In Python, the comparison operators are not directly commutative, but > have a reflected pair corresponding with the swapped order. However, each > object could implement entirely different logic, and thus no commutativity > is enforced. " > > I... have no idea what that sentence means. Please clarify. > I added a more explicit explanation of this. Basically, comparisons are not "commutative". They have a reversed relationship with some other comparison. `$x > $y` gets *reflected* to `$y < $x`. Equality comparison is its own "reflection". The reflection for the spaceship operator is `$x <=>$y === ($y <=> $x) * -1`. > - It doesn't seem to be explicitly stated, but I presume developers can > type their operator methods as `mixed` if they are so inclined? (I don't > see why they would, but they could.) > Yes, they are not prevented from doing this, but they must do so explicitly with the `mixed` literal when typing the parameter. The return value is not forced to be typed (that is, no type declaration is treated like mixed, even though in the engine it's a separate flag), but can also be mixed if they wish. - You refer to InvalidOperator as an exception, although it extends Error. > That's technically incorrect. It is a new *throwable*, which extends > error. Please use the correct terms. (The design is fine, IMO, but it > should have the correct terminology.) > Thanks. I've cleaned up all the references I believe. - I think sticking to compareTo() is probably wise. I can see all sorts of > abuses of the other comparison operators if they were allowed... and I'd > probably do some of that abuse myself, frankly. :-) > > - Please make sure via tests that __compareTo() is triggered by sort() and > friends, as that's the main use case for it: Making sorting objects just > like sorting anything else, without having to provide a separate, > independent comparison algorithm. (Basically, replacing usort() with > __compareTo().) > Yes, I have three existing tests in the engine to resolve, then I will move on to improving and expanding the tests for this feature. :) > > Open questions: > > > > - Should this RFC include the bitwise operators or remain limited to the > > minimal set of math operators? > > This should IMO be decided based on how easy they are to implement. If > it's trivial to include, and uncontroversial, sure. If it's contentious or > hard, punt to a later RFC. > They are very trivial to implement. If anyone has an objection to this specifically, I'll of course take that into consideration. I am not planning on entertaining the idea of any of the miscellaneous operators, reassignment operators, or logical/boolean operators in this RFC though. That seems too free form, especially when the PHP community won't have any pre-established community standards around using them prior to this feature. > > - Should the == operator fail-over to existing behavior for all classes > if > > unimplemented, not just internal ones? (This would reduce BC break, but > > make catching implementation errors more difficult for userland code.) > > It would help if you documented the current behavior in the RFC, because I > had to go test it to see what it was. :-) > > I'd say yes, keep the existing behavior. I actually have a few tests > right now in a serialization library that check if $foo == > unserialize(serialize($foo)); (Not those functions, but in concept.) > Yes, I actually already committed the change for this and updated the RFC. There are too many problems involved in trying to error unimplemented == operators, and that might not be desirable behavior anyways. Jordan --000000000000d4fe4f05ca097777--