Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115764 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89965 invoked from network); 19 Aug 2021 08:06:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Aug 2021 08:06:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 458B21804AD for ; Thu, 19 Aug 2021 01:38:59 -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-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 ; Thu, 19 Aug 2021 01:38:58 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id x27so11163350lfu.5 for ; Thu, 19 Aug 2021 01:38:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=bKizLxnZVCYpIuLnhULhLBce3mwN5gzMWAaDSqjghUU=; b=gLVFCXcp+J+K/bWM2wPfg4Qtwx4weV4RpQQ3mx447O9plHJ2a6zu5DOa4D50ZjM79j SgQXHWjRwxGT1SkZ1ub92U5wxFIaBUyxJDHUguQh737Wvte74pssmIajzb5HWyHxL5Gd OWWoi8Sw3DLUyW0bxA5DbDtnVoOauLURMdG3MZGwuOF6we5OdRKZ1/kGBGLSI4Bra2gN LNzWC8SRYK3YKZMH62K5VnS1YCvxzx0tKW4K6kJNFr1pVETA2aZ9gqLuCsU2J4iVfCqU /nFg20v4wj7/VUkNGmLijchb1+SQGRpzfYn1oq7jBClOV9gUH30Tq6POvAAP1E8ZSnls J0oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=bKizLxnZVCYpIuLnhULhLBce3mwN5gzMWAaDSqjghUU=; b=EYsTBFjAJjTV4A4jckG93Km4qMLGXnE8ZKnDC9rwNFHU0hv9cGL16M/o7jUTcve2uk CwW3b4zzx5T2zWxrRWb8AE/jqjSByffxIwG4oGGaN68ICqcLp08Gcko1pqjRJ36PQjfq abcFEstyn/Cvz4+elkapLgJ9OVKo/tZ6hUs34JJGIn6lIS2FKTaPW9hp7i5sjZ1JboOr IjagBMPNLfDCAT+YeFd+A+XYn7G/DWyE5l0ix7mt4sgjFu9Em5lmp+Ah06qRdFcKR0l5 YyEcoSO7ETgYjFEJQcFhWne0nz/Ajs6gvPBeb210icKVxWMRjy8+sFt/fYvKmvyOmnny ndXQ== X-Gm-Message-State: AOAM533SBzcv3nKbjAI/SuoFiBFwgg72z87UZCJ3jXladv0mQzjAzsel HAjLJad2Uo/vgFpm9vw52uywYvldPtdh740tfezyiMkVnegnvA== X-Google-Smtp-Source: ABdhPJznxecDBomJfaaYgKuOxRnxVCzSjH3F1YSqE0b42SORK9cqUKKDVVr++MIdzDf08MBkMw03TW7fsWRPRzTA19M= X-Received: by 2002:ac2:58c5:: with SMTP id u5mr9349062lfo.603.1629362335958; Thu, 19 Aug 2021 01:38:55 -0700 (PDT) MIME-Version: 1.0 Date: Thu, 19 Aug 2021 01:38:46 -0700 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000003ecc0105c9e57baa" Subject: [RFC] User Defined Operator Overloads From: jordan.ledoux@gmail.com (Jordan LeDoux) --0000000000003ecc0105c9e57baa Content-Type: text/plain; charset="UTF-8" Hello again internals! Thank you all for the feedback that you've provided on my initial inquiries about this feature. Several bits of feedback I've received have resulted in changes to the proposal as it stands and made this a better proposal. First, the RFC has been moved to the wiki: https://wiki.php.net/rfc/user_defined_operator_overloads The RFC is very long, as it contains a lot of research into the topic, which is part of the reason I'm bringing it to discussion so early. Work has also begun on an implementation, for which the draft PR can be viewed here: https://github.com/php/php-src/pull/7388 As the PR indicates, there is still a ways to go before a finished implementation is ready, however the RFC document appears complete enough now to be opened for discussion. Again, as I've indicated before, I'm taking my time with this, so there won't be a vote prior to a final implementation being reviewable. It is possible that this RFC will involve some changes to opcache as well as new opcodes for > and >= in order to maintain consistency of operand precedence in execution, and that's something I want to take my time with and listen to a lot of feedback on. Its impact is currently unknown as the implementation for it is unfinished. Some key points for this proposal as it is currently: - TypeErrors from the operator methods are thrown immediately instead of being suppressed. - The parameter for the other operand must be explicitly typed; an omitted type is not considered mixed for these methods. Reasoning explained in the RFC. - Type coercion still occurs as normal when strict types is not used. - The return type of the == operator function is restricted to be a bool. - The return type of the <=> operator function and all implied operators (<, <=, >, >=) is restricted to be int and is normalized to -1, 0, 1. - User classes are forced to implement overloads to work with the supported operators, resulting in an InvalidOperator exception if unimplemented. (This is a BC break, but impact is anticipated to be low) - The <=> operator instead falls back to the existing comparison logic if the overload is unimplemented. - The special cases of $obj == null and $obj == false return the expected bool value even if the overload is unimplemented. - Internal classes silently fail-over to existing operator logic. (This mainly affects ==). - Enums are allowed to utilize operator overloads, but unlike other classes, are not forced to and fall back to existing logic for comparisons. - The proposal is for dynamic methods instead of static methods, with reasoning explained in the RFC. - The identity operator === is not overloadable to prevent possibly terrible misuse. - Interfaces are not suggested or proposed, with reasoning explained in the RFC. Open questions: - Should this RFC include the bitwise operators or remain limited to the minimal set of math operators? - 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.) - Should implicit interfaces be provided for the overloads? (Actual interfaces cannot be provided except for if my previous Never For Parameter Types RFC were adopted.) Thank you for all the feedback and help so far. Jordan --0000000000003ecc0105c9e57baa--