Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121055 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 38598 invoked from network); 13 Sep 2023 17:30:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Sep 2023 17:30:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9D5C21804AC for ; Wed, 13 Sep 2023 10:30:34 -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=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE 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-f48.google.com (mail-ua1-f48.google.com [209.85.222.48]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature ECDSA (P-256) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 13 Sep 2023 10:30:31 -0700 (PDT) Received: by mail-ua1-f48.google.com with SMTP id a1e0cc1a2514c-7a50a1d1246so46964241.3 for ; Wed, 13 Sep 2023 10:30:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694626230; x=1695231030; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=AP0zAa1RclGEncU0YS/DK7oW9LwZDhjQoy0Rtm76eVY=; b=ML/si2qnuBeu59laREsfed4VwAWTn4cCcsCN2BWUl8cZSqaPM6LjplyRPxICN0o6ya OIpCiKeAE9GVxVtHeKNiIddx9xOuz9AhTyIuX7+hBrQYLEnzE9YHccUn/Pb88iFGeuOq GDp8BYhNfIufou1n/lBb3+Pf4ng7y8JQ+toiNWnA91ZaasLF8bkJwTcyaojXZgjV1dTm ASjrYdOZcRn/kHzIrYCZCmayd4bofOEBDyaVHAlNWGrTUnvuZ7QcRqBNFCaOaLGLODy3 Toxjanj24c2FwIEZABS77/wmZ0LHm2jAq8i6SVqx0N/vpS5OUi4yHZ2LQiRyWKQRLtk/ EeTg== X-Gm-Message-State: AOJu0Ywve26Tq7pCwYctYCvzd75XioDJFAVXSqd0aL9a8drG/iujeCiw +jOp64UbMGzDkPj17v9ukUt0t7raMACIH87Ipxc= X-Google-Smtp-Source: AGHT+IGCJCreP75bavxLiHkqZmhxXuf5dW0Ac0q+4uaH3y8BNZ5DSOfHri/RAD/PAlcn2cjGUjr6ZAc9L1KPB1VK4UM= X-Received: by 2002:a1f:ec84:0:b0:48d:eaa:45c4 with SMTP id k126-20020a1fec84000000b0048d0eaa45c4mr3055223vkh.7.1694626230102; Wed, 13 Sep 2023 10:30:30 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 13 Sep 2023 11:30:14 -0600 Message-ID: To: Ilija Tovilo Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][Draft] Match block From: levim@php.net (Levi Morrison) > 2. Support block expressions as a language-level concept, analogous to > https://doc.rust-lang.org/reference/expressions/block-expr.html This is my personal preference. As I mentioned in chat, if we go this route, there is a notable pain point: in "short" closures which have blocks, how do you capture by reference if needed? Consider this situation: // function array_push(array &$array, mixed ...$values): int; $array = []; $value = fn ($x) => array_push($array, $x); I have an incomplete idea about allowing `&` at the call-site if and only if the corresponding parameter is taken by reference: // Ok, $array is taken by reference in array_push $value = fn ($x) => array_push(&$array, $x); // Not ok, ...$values are not taken by reference in array_push $key = ''; $value = fn ($x) => array_push(&$array, &$key, $x); Then, when `&` is given at the call-site, that variable is captured by reference. If they don't want it to be captured by-reference, then they have choices: // Choice 1: omit the `&` at the call-site. $value = fn ($x) => array_push($array, $x); // Choice 2: copy the array. // Assumes blocks are allowed. $value = fn ($x) => { $copied = $array; array_push(&$copied, $x); } It would still be valid to omit `&` at the call-site in PHP 8.x so a transition can be made. Theoretically, we could make it required in PHP 9.0 or 10.0. Nikita started an RFC for this 5 years ago: https://externals.io/message/101254 I do not know exactly why it was not voted on. There was substantial drama in the discussion that was, in my opinion at least, not really relevant to the RFC contents. The original motivations still stand: 1. The code becomes more readable by humans. 2. The code becomes more readable by analyzers and optimizers. And now we have another reason: 3. Enables auto-capture by-reference when it is necessary.