Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116620 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 62242 invoked from network); 11 Dec 2021 14:14:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Dec 2021 14:14:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5A7271804D4 for ; Sat, 11 Dec 2021 07:15:48 -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-f51.google.com (mail-ua1-f51.google.com [209.85.222.51]) (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 ; Sat, 11 Dec 2021 07:15:47 -0800 (PST) Received: by mail-ua1-f51.google.com with SMTP id n9so2808550uaq.10 for ; Sat, 11 Dec 2021 07:15:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=basereality-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=8IVvLn4CD4Tinm68X8aLDvivYVJA/+OP6t7Wc12js2o=; b=EHvCNVmmNB4JALTPTBEwLjT2Y+nMoRvpccp2cZ9Z3UaFYQHDVPfC7i1d++7QMQA1mO JDnIm9pq+KsWvx2uwolx3ZzjA29nOTfgTJQO7yBbp9919FyLumOczOBKdMgRuyy1mEX4 t5K7Vpbsw8K6UOXhpkROm7zVRgZ9cu+s5kxpc+8wSUpuFXjCWjVMQZ30hzvCgHfFltbA 92EaLTHojRR3FBgRzsUL1zWkI2F1dwNoo9AwkLkpWNbDQzgqExAIyW6g6Ob4NgdWGEF5 ptp9XpG8U0DIviF0w19znPW/zoRsscVEeu8YoFoFvEMSx/E6PYxZqF6hnUuq3Dc6Yvhx CsGQ== 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=8IVvLn4CD4Tinm68X8aLDvivYVJA/+OP6t7Wc12js2o=; b=hcRpwH4sd42LpmrmpF8c3s+4XDZIXZoQO4JhLy1SCXDM5lJ9a7rh+T5AW6fRf+usjb f1NjOUTDJieqI5GqOz1GE2SLTQjx21PLRfAcPwyQkVbGzU0hxMU+yk6KpWhWn+EV08cu n/b+coiO33JPkZRbyI3xz3PzEqIVXQ6/PuHRUcccsBDjFddCtbag57IhslNwSUi8aRk1 ZYEEVf+VdvF2pzPAv7y0ijNCiKPF4Pqmg0qMFLEfogLVxZrOi4z9PkBepSLgV5APCTfk R6x98ZYC7jA3eeLPe3KFw60fd4OxLzvAtiweXRCP8+w/q+p4pSLV6EIeDy3TSuwK30hN 6sZQ== X-Gm-Message-State: AOAM532sJjvOy9GuJzM2bW/BYW5uQqIuLZi3i7vok6Jo2fT/lSaCm8TK 9tQJErewtGsf9ZbER6k+7faaOMN7mI2Qy9pu32Akiw== X-Google-Smtp-Source: ABdhPJw9jtHgSvdvk2lad1XZdOQh7Qog2CmRDpl/Jletp/jGlpy4hHWdNmtxAywm/IuFG6SAdeZPDP7tBBUYRemevEI= X-Received: by 2002:a05:6102:2910:: with SMTP id cz16mr22043955vsb.9.1639235746608; Sat, 11 Dec 2021 07:15:46 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 11 Dec 2021 15:15:34 +0000 Message-ID: To: Jordan LeDoux Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] User Defined Operator Overloads (v0.6) From: Danack@basereality.com (Dan Ackroyd) Hi Jordan, On Thu, 9 Dec 2021 at 20:12, Jordan LeDoux wrote: > > Hello internals, > > I last brought this RFC up for discussion in August, and there was > certainly interesting discussion. Since then there have been many > improvements, and I'd like to re-open discussion on this RFC. In general I'm in favour of this RFC; a few months ago I was programming something and operator overloads would have been a good solution, but then I remembered I was using PHP, and they haven't been made possible yet. However.....I think the new 'operator' keyword is probably not the way to go. Although it's cute, it has some significant downsides. There are quite a few downstream costs for making a new type of methods in classes. All projects that analyze code (rector, codesniffer, PHPStorm, PhpStan/Psalm, PHPUnit's code coverage annotations etc) would have to add a non-trivial amount of code to not bork when reading the new syntax. Requiring more code to be added and maintained in PHP's builtin Reflection extension is also a cost. That's quite a bit of work for a feature that has relatively rare use-cases. I just don't agree/understand with some of the reasoning in the RFC of why using symbols is preferable. "In such a situation, using magic methods would not be desired, as any combination of symbols may be used for the new infix. The restrictions on function names, such as needing to reserve the & to mark a function as being by-reference, would place limitations on such future scope." I don't get this. The magic methods in previous drafts of the RFC don't have a problem with & as the methods are named with 'two underscores' + name e.g. __bitwiseAnd. That does't appear to cause a problem with an ampersand? "By representing the implementations by the symbols themselves, this RFC avoids forcing implementations to be mislabeled with words or names which do not match the semantic meaning of that symbol in the program context. The name of the function (e.g. __add) always refers to the symbol used where it is used, not what it is doing. If the code is `$a + $b` then that is an addition operator, when the code is read. If I was reading the code, and I saw that either $a or $b were objects, I would know to go looking for an __add magic method. " '// This function unions, it does not add'" Then that is probably an example of an inappropriate use of operator overloads, and so shouldn't be used as a justification for a syntax choice. # "Non-Callable - Operand implementations cannot be called on an instance of an object the way normal methods can." I think this is just wrong, and makes the RFC unacceptable to me. Although most of the code I write is code that just performs operations as I see fit, some of the time the operations need to be driven by user data. Even something simple like a calculator-as-a-service would need to call the operations dynamically from user provided data. I also have an aesthetic preference when writing tests to be explicit as possible, rather than concise as possible e.g. $foo->__add(5, OperandPosition::LeftSide); $foo->__add(5, OperandPosition::RightSide); instead of: $foo + 5; 5 + $foo As I find that easier to reason about. cheers Dan Ack /congratulations on stunning the audience into silence otherwise though.