Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116822 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 86442 invoked from network); 5 Jan 2022 19:35:27 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Jan 2022 19:35:27 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D043F1804E3 for ; Wed, 5 Jan 2022 12:42:58 -0800 (PST) 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-ua1-f47.google.com (mail-ua1-f47.google.com [209.85.222.47]) (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 ; Wed, 5 Jan 2022 12:42:58 -0800 (PST) Received: by mail-ua1-f47.google.com with SMTP id v12so640322uar.7 for ; Wed, 05 Jan 2022 12:42:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NLTOslMA5pa987jXWRQNuT/sJuq4HmYhEM2pQ2zlaCQ=; b=ARCL4iWuJ803nqcYsUptlqUeaRKiU3JRG9QU7HM6QDklXOxKT5Tw2vJxTV7wjHrUWK HSgo6w1ppKIh0hwiS9cDJtfDdGaOgaNSZzdOfxKz0VFUuCOr3vHxl8U8I3S11FRcWIi8 JzAPqNhywul7B4Xwk4Vt0YKNT5L821KKApxt8vJDBXJ1qwFO1NETAq+ltxOxtJv/eEgd dM3m5K5tVpCdBBkKyRJNXkyREWORlmJEIHclGyO2JThTx7MLEIl4Lg4JHqMbV8HdHl9u 5tBYZRz8mIOmFCC+Cnu1mXAsm0neuWkgQdynAzdki+MQ0G56uSLJqDhyiMXpNcYLWQlN AQzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NLTOslMA5pa987jXWRQNuT/sJuq4HmYhEM2pQ2zlaCQ=; b=apM7cHxsnopctTtAa4uk795961jg4QZR840ek+FlIoCeZWtNJWi6Vqj3587qsTRitS Afq6RTE77xHKRplwmnc2VPlfhXktgj5k5YLpOl63++hkDOKsaFRD3fVIsfj136RuMBaW aVBRGonjj875eyBeeYDtWmQL2koV+aQRifGf9zCxZS9YtI8ZFulyTy0z8DpXXQYUO5kW d3kVHmrVxPRAxK8wlER/YSxOmY6uIjzFMWb5FkqjSl/6PN1M8yITJSU5mcAKqi8FLHgU dL1uMwfl6mZ6dL21ucBFeRpoQlfw1QcVIMBN5BP2yNgtVCD8dxrGOWK+0LbQo4gI2c/2 VyPA== X-Gm-Message-State: AOAM532BI+jtHxZEBBFMfdcI6P+7R3ojEqp3qrJWqZgr6iam3GoCJJPG k+grtZc/NyvawIBfpzFhWDh1XcvSN0RIM3iXJAk= X-Google-Smtp-Source: ABdhPJywHUmGm4GhOuLxZqOFrVX0fiT16hF7qy9cZMS/DUItvjf4EBMKHJvuYY9INIqfBtCtkSpcpNMotEXYYDcdjS4= X-Received: by 2002:a67:bb01:: with SMTP id m1mr18778739vsn.48.1641415377652; Wed, 05 Jan 2022 12:42:57 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 5 Jan 2022 15:42:46 -0500 Message-ID: To: Andreas Hennings Cc: Jordan LeDoux , PHP internals Content-Type: multipart/alternative; boundary="00000000000083908605d4dbcc4d" Subject: Re: [PHP-DEV] [VOTE] User Defined Operator Overloads From: chasepeeler@gmail.com (Chase Peeler) --00000000000083908605d4dbcc4d Content-Type: text/plain; charset="UTF-8" On Wed, Jan 5, 2022 at 3:24 PM Andreas Hennings wrote: > On Wed, 5 Jan 2022 at 20:11, Jordan LeDoux > wrote: > > > > > > > > On Wed, Jan 5, 2022 at 6:33 AM Andreas Hennings > wrote: > >> > >> Hello Jordan, > >> do you have any thoughts about these symmetric/left/right modifiers, > >> to get rid of the $operandPos parameter? > >> > >> To me, the parameter could be the kind of thing where in hindsight we > >> ask "why?". > >> > >> Also, can we drop the "public" modifier, if we do a new version of the > RFC? > >> > >> Cheers > >> Andreas > > > > > > It's a totally different design concept (symmetric/left/right) and I've > been going over the design implications before I responded. For instance, > wouldn't this be a special case of method overloading? You're overloading > according to a modifier, not the typing, but there is that to contend with. > If someone defined `symmetric operator +` and `left operator +` which would > be used? (My feeling is left as its more specific.) > > You would not be allowed to provide both on the same class. > > A class can provide either no operator, or the left operator, or the > right operator, or both. > A "symmetric" operator is a shortcut to providing both left and right, > but using the same implementation. > A class with a "symmetric" operator cannot also have a "left" or > "right" version of the same operator - this would be the same as two > methods with the same name. > However, if the parent class has a "symmetric" operator, you _can_ > override the "left" and/or "right" version in the subclass. In that > case, if the subclass provides "left", then the parent "symmetric" > implementation would only be used for the "right" direction. > > I am writing "right" and "left" here, but perhaps "normal" and > "inverted" would be less ambiguous. > Also, perhaps instead of "symmetric" we could write "left right". > > Perhaps this code will clarify: > > class C { > symmetric operator * (int $other) {..} > # left operator * (int $other) {..} // -> Error, already defined. > left operator - (int $other) {..} > right operator - (int $other) {..} > left operator / (float $other) {..} > left operator % (D $other) {..} > } > > class D { > right operator % (C $other) {..} > } > > (new C()) * 5; // -> symmetric operator * > 5 * (new C()); // -> symmetric operator * > (new C()) - 5; // -> left operator - > 5 - (new C()); // -> right operator - > (new C()) / 5; // -> left operator / > 5 / (new C()); // -> Error, no operator provided. > > (new C()) % (new D()); // -> left operator % provided by C. > (new D()) % (new C()); // -> Error, not supported. > > This means, the "right operator" will only ever be called if the left > side does not provide a "left operator". > > Basically this is not so different from your RFC. > Just instead of having to do a "if ($operandPos === > OperandPosition::LeftSide) {..}" inside an implementation, we are > providing two separate implementations. > > > > How would they be stored within the zend_class_entry? Since they would > technically have the same name, something would need to happen for them to > not be in the function table. > > The right and left version would need to be distinguished somehow > internally. > I would say "left" is the default, and "right" has a modifier to its name. > Perhaps for symmetric operators, we could store two distinct entries > internally, if that makes things easier. > > If nothing else symmetric could be handled by having the compiler replace it with left/right version that executes the same code > > > > The public modifier is not required (as stated in the RFC), you can just > add it if you want. Are you asking for `public operator` to produce a > compile error? > > That would be the idea. > Perhaps others will disagree. > Allowing the "public" introduces a meaningless choice that people can > argue about in their code style, and have pointless git commits where > they add or remove the modifier based on preference. > Of course the same could be said about "public" in interface methods. > > I personally don't like it when I don't have a consistent look to my code. One of the things that bother me is code like: function foo(){} protected function bar(){} function baz(){} I want everything to have a visibility indicator for visual consistency. That's why I always use public in my interface methods as well. So, even though it won't cause confusion if omitted for an operator, since it can only be public, I still think it should be allowed. > -- Andreas > > > > > Jordan > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > -- Chase Peeler chasepeeler@gmail.com --00000000000083908605d4dbcc4d--