Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116660 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11014 invoked from network); 16 Dec 2021 18:22:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Dec 2021 18:22:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 28405180540 for ; Thu, 16 Dec 2021 11:25:10 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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-f41.google.com (mail-ua1-f41.google.com [209.85.222.41]) (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, 16 Dec 2021 11:25:09 -0800 (PST) Received: by mail-ua1-f41.google.com with SMTP id u40so208798uad.1 for ; Thu, 16 Dec 2021 11:25:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=DnI/7oHD5xwQQl0u7EDWxorokYNej9Y+hj2k6ZpnqtI=; b=ph0hkwOqQidV9aE27x430j6CNDPrDvGMM2k1CvkWgt7YCbxvbIaqc5sOmu5dcpbzm/ HAAGofXW82x4Of+1X9V4VEGzb6YuJxLhBtd01I8CCCA5yGbvRbHm0AaKPVzj1GEv9623 A/hxEF/8pIg/XKM1/jePfzffAhOPKNnq4jGpMUUUyDxMrT1eeq8XHEp0NZuzZMUGt4EI rM9ZLRvOdCS3MS69+C5JUPKSbSpg0gvyOkfMkS3+41/NyELeWTsY0pDs+ffNub5eLPLV GUMUvnRGfB/6r+063BfeBHmHYUTus6QAZhho0+fnaBNGe7FaWaYEVceyKX2ynIKRd5+G SW1Q== 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:content-transfer-encoding; bh=DnI/7oHD5xwQQl0u7EDWxorokYNej9Y+hj2k6ZpnqtI=; b=NcvvfblgCz2QZouAHPDtmpgdPkkM+QVIL0EI2iFQV9THBR6aUrStN9r52x8WDU/VSy SwLDCXQjdKeGA0dSfOOVvkNQ4Ddr96zRdpAnvTknFUTxWYZEgpCrR9ClkHKKuGNx8Ij0 q4Le+tl60MzBEW85q7ZdQG9kvaJewNkV1YPUywMHN3SSJ5cuAr+Q6N19RsF8xLfv66wX YDwKWzrX6YtJ2s8F2DGxGNJt1IIl/tLqNK5GVWa33FfEmQAMuXbEys354sXrT+Oy0HJc romxYi6gZSv7Rws3nh4uq1y2rsNBDJFI1PpK/tN6YlpXffo0i+t26AC8WKzN9TaVTCXB zWuQ== X-Gm-Message-State: AOAM532wOYKXV1e8q1Ikq4z9KQ1o4ccFjQvkhkAw3T3xIwXkKQnCNSzU AbqHP4Asth7+MiApU/JSdrioLxOiKTeZcXi+62sUcUpUmPAF1sd3 X-Google-Smtp-Source: ABdhPJyAL2/vRKXuEdrDzT/F9x7LVioG14rGZbw93PvSxZiMRBsfkFpS757A5a5jCTt5zb9MjbEW9Wh8+YOobiBrIYA= X-Received: by 2002:a67:f241:: with SMTP id y1mr1364831vsm.70.1639682708554; Thu, 16 Dec 2021 11:25:08 -0800 (PST) MIME-Version: 1.0 References: <44b3fb4b-4693-1639-c8c0-5e17296c196e@gmail.com> <4b58c011-ed87-ba87-201d-0cf8e4116c6f@processus.org> In-Reply-To: Date: Thu, 16 Dec 2021 20:24:57 +0100 Message-ID: To: Dan Ackroyd Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads (v0.6) From: andreas@dqxtech.net (Andreas Hennings) On Thu, 16 Dec 2021 at 19:20, Dan Ackroyd wrote: > > On Thu, 16 Dec 2021 at 12:21, Andreas Hennings wrot= e: > > > Methods and functions have searchable and clickable names. Operators do= n't. > > The "searchable" applies to grep searches in code, but also google > > That's one of the reasons why I prefer a magic methods based approach. > > function __plus(...){} > > can be searched for...and for future scope, something like: > > function __union(...){} > > is more self-documenting (imo) than: > > operator =E2=88=AA(...){} > I don't mind using magic methods for this, compared to an operator keyword. It is also what I found is happening in python. However, this does not give us searchability in the calling place, only where it is declared / implemented. > > > Lack of real parameter overloading > > Unlike C++ (or C?), we don't have real method/function overloading > > based on parameters. > > Java is probably a better comparison language than C. > > I have a note on the core problem that method overloading would face > for PHP here: https://phpopendocs.com/rfc_codex/method_overloading > > But although they both involved the word 'overloading', operator > overloading, and method overloading are really separate features. I see the distinction in overloading based on the object type on the left, vs overloading based on parameter types. For a method call $a->f($b), the implementation of ->f() is chosen based on the type of $a, but not $b. For an operator call "$a + $b", with the system proposed here, again, the implementation of "+" will be chosen based on the type of $a, but not $b. For native operator calls, the implementation is chosen based on the types of $a and $b, but in general they are cast to the same type before applying the operator. For global function calls f($a, $b), the implementation is always the same. In a language with parameter-based overloading, the implementation can be chosen based on the types of $a and $b. This brings me back to the "symmetry" concern. In a call "$a->f($b)", it is very clear that the implementation is owned by= $a. However, in an operator expression "$a + $b", it looks as if both sides are on equal footing, whereas in reality $a "owns" the implementation. Add to this that due to the weak typing and implicit casting, developers could be completely misled by looking at an operator invocation, if a value (in our case just the left side) has an unexpected type in some edge cases. Especially if it is not clear whether the value is a scalar or an object. With a named method call, at least it is constrained to classes that implement a method with that name. > > > we cannot have the conditional type hints. > > btw you can just say 'types'. > > Unlike some lesser languages, in PHP parameter types are enforced at > run-time; they aren't hints. I believe all references to hints (in > relation to types at least) have been removed from the PHP manual. Ok, what I mean is return type declarations. In a class Matrix, operator(Matrix $other): Matrix {} can be declared to always return Matrix, and operator(float $factor): float {} can be declared to always return float. However, with a generic operator(mixed $other): Matrix|float {}, we cannot natively declare when the return value will be Matrix or float. (a tool like psalm could still do it) But even for parameters, if I just say "type" it won't be clear if I mean the declared type on the parameter, or the actual type of the argument value. > > cheers > Dan > Ack