Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116679 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 5601 invoked from network); 17 Dec 2021 20:21:50 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Dec 2021 20:21:50 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B76761804D4 for ; Fri, 17 Dec 2021 13:24:37 -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=-1.2 required=5.0 tests=BAYES_05,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8412 83.65.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mail02.x-net.at (mail02.x-net.at [83.65.141.138]) (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, 17 Dec 2021 13:24:36 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail02.x-net.at (Postfix) with ESMTP id 81E0D3801E6 for ; Fri, 17 Dec 2021 22:24:34 +0100 (CET) Received: from mail02.x-net.at ([127.0.0.1]) by localhost (mail02.x-net.at [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id XQQwcJGlWNfU for ; Fri, 17 Dec 2021 22:24:33 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by mail02.x-net.at (Postfix) with ESMTP id B6540380477 for ; Fri, 17 Dec 2021 22:24:33 +0100 (CET) X-Virus-Scanned: amavisd-new at x-t.at Received: from mail02.x-net.at ([127.0.0.1]) by localhost (mail02.x-net.at [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 1cVQtL7UaUVQ for ; Fri, 17 Dec 2021 22:24:33 +0100 (CET) Received: from [127.0.0.1] (193-80-10-121.adsl.highway.telekom.at [193.80.10.121]) by mail02.x-net.at (Postfix) with ESMTPSA id 7B9453801E6 for ; Fri, 17 Dec 2021 22:24:33 +0100 (CET) Date: Fri, 17 Dec 2021 22:24:33 +0100 To: internals@lists.php.net User-Agent: K-9 Mail for Android In-Reply-To: References: <44b3fb4b-4693-1639-c8c0-5e17296c196e@gmail.com> <4b58c011-ed87-ba87-201d-0cf8e4116c6f@processus.org> Message-ID: <5DB9C8A8-C760-4D3D-9DDC-54B5A135358F@dafert.at> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads (v0.6) From: mel@dafert.at (Mel Dafert) Hello internals, >register_operator(*, function (Foo $lhs, Bar $rhs): Foo { =2E=2E=2E}); >register_operator(*, function (Bar $lhs, Foo $rhs): Foo { =2E=2E=2E}); >register_operator(*, function (int $lhs, Foo $rhs): int { =2E=2E=2E}); > >But this just brings a new set of problems, including visibility issues >(i=2Ee=2E can't use private fields in the implementation), and the fact t= hat >this requires executing a function at runtime rather than being defined a= t >compile time=2E Since this is going deeply into magic land anyways, we could go another st= ep further and make this a builtin/"macro" that does happen at compile-time, but also= can impose additional restrictions on what is allowed - namely, that the registered function must not be inlined but a s= tatic method on one of the arguments=2E For example: (syntax completely imaginary here but slightly inspired by ru= st): register_operator!(+, lhs: Bar, rhs: Foo, ret: Bar, Bar::addFooBar); register_operator!(+, lhs: Bar, rhs: Bar, ret: Bar, Bar::addBar); register_operator!(+, lhs: int, rhs: Bar, ret: int, Bar::addBarInt); register_operator!(+, lhs: Foo, rhs: int, ret: Foo, Foo::addFooInt, commut= ative: true); with class Bar { =2E=2E=2E public static addFooBar (Bar $bar, Foo $foo): Bar { } // etc=2E } Advantages: - Explicitly named methods that can be called/tested separately - Slightly improved searchability - grepping "register_operator" will show= all operator combinations inside a code base - Cannot implement operators for arbitrary classes that one does not own -= the method must be from one of the operands - Multiple distinct methods per operand/class without full method overload= ing - No restrictions around having scalar types only as rhs If I am not mistaken, the engine should also be able to typecheck the meth= ods to ensure that the types are correct, and additionally also be able to dis= allow overlaps (eg=2E defining Foo+Bar commutitatively as well as Bar+Foo), which should throw an error as soon as the second definition is encountere= d=2E Disadvantage: This sounds like a lot of work to implement, and I am not su= re if the checks are even possible the way I'm imagining them (with classes b= eing loaded on demand, etc=2E)=2E Also, this syntax would definitely need work, I just wanted to point out t= hat on the drawing board, many of these design problems are solvable=2E Whether they are worth the effort, and whether this is a good idea at all,= is left for others to decide=2E Regards, Mel