Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120609 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 24151 invoked from network); 17 Jun 2023 11:26:49 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Jun 2023 11:26:49 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 11FF7180340 for ; Sat, 17 Jun 2023 04:26:49 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 17 Jun 2023 04:26:48 -0700 (PDT) Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-6b2e1023f30so1612046a34.1 for ; Sat, 17 Jun 2023 04:26:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687001207; x=1689593207; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=UeHPhO9DlWNh1JNxwE0JPpASOyqy3FrPQMG8b1JcMUY=; b=q3clkaul9IEySvKOBP+oocUezbAMhYyXtZpmvrpd/DyjHXlqUKV7p27TUkaTNk5hKp VqQD7TsAiLG4m9ymU0S0pk41NsNYjWNZRVpuRPFZRB1Py4KECo6P/O+Eg1S0qxKfx8lE uiREILpyCgw88aP4zkqV2vBHQbtEZh00BURYuwL/GJt3FzL6U6ubgjlmVu/XU3Wsu6G5 LmcBsrth1v3SzHOnYDaexjtksKA5AGENHRgV2Dh2XKuIHILZ/PP7WYdMKw74B4Kvx0NL SZ63obR9wKMKKXHaH7vWkua0O9WMD6Y00aWsDpBza9EaSwwpepnHsVzmNnijOZfcMrTG 1XRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687001207; x=1689593207; h=content-transfer-encoding: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=UeHPhO9DlWNh1JNxwE0JPpASOyqy3FrPQMG8b1JcMUY=; b=GbHD3Z4ShRR0CX34UH9FIS/RUIrL8iekNACihAhYSYn22qvLfRxjcGg8HQDGeTM3ju mhigH5+QwIsNcLORbpVaRkSnfpjODJIpwHkuhhu6rSSaN/b/q1mY7W5dMjocbHnZs60b Uyv8I4Sx1m21KLHcgJx66eSg7JgnAQC/letftc3hYLrIp8DzWrNX84DV7CPWmoGG4jXa 4W4AhKmpmO5zQp15dlrSdGCEBqFqMcKLcu6tmjBJ7ccrVLe9iheWFy+Vzhg+IG8YAi3I tsyOSgY/D7tXhFU7Z2DbiIxkvwOEzllXFL2hIvV8eZNPivds8kf+chY2gTaj3XboYYpQ jHhA== X-Gm-Message-State: AC+VfDwXsX4P6FSk2Mhjxc1FpHqegrZGD+eJOzfiru0cso8bkzy1OKm3 yidQn3YcDB85CH9evST1vzjYi0y/YMXTOAUuS3i9q5jCSfM= X-Google-Smtp-Source: ACHHUZ43D5v/5voc432+5FBUj2yXS7/rOwEES2g1qvLsXoiw/b5OmzJkAxh/jFJ4R5fEdJn78jJ/tyzLUoSCgjS7DZQ= X-Received: by 2002:aca:1a17:0:b0:39e:ae60:a06a with SMTP id a23-20020aca1a17000000b0039eae60a06amr3914685oia.5.1687001207480; Sat, 17 Jun 2023 04:26:47 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 17 Jun 2023 13:26:36 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Expression code blocks From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Andreas On Fri, Jun 16, 2023 at 9:23=E2=80=AFPM Andreas Hennings wrote: > > Hello list, > I don't know if something like this was already proposed in the past, > I did not find anything. > > Sometimes it would be nice to have a code block inside an expression, lik= e this: > > public function f(string $key) { > return $this->cache[$key] ??=3D { > // Calculate a value for $key. > [...] > return $value; > } > } This has been discussed a few years back when match expressions were proposed. I originally wanted to include support for code blocks along with expressions to offer a more complete alternative to switch statements. The other major use-case for block expressions are arrow functions. Unfortunately, a general solution seems suboptimal due to the subtle semantic differences. See this message for my detailed thought process. https://externals.io/message/109941#109947 I believe it would be best to address blocks for match arms and arrow functions separately. I don't believe blocks for general expressions are that useful in PHP due to the lack of block scoping. Your suggestion to make the block a separate closure could avoid that (as well as the optimizer issue mentioned below) but comes with new issues, like making modification of captured values impossible without by-ref capturing. It seems confusing that fn {} is auto-executed while fn() {} isn't, as the former looks like a shortened version of the latter. fn() =3D> fn {} would also look quite weird. match ($x) { 1 =3D> fn {} } seems ok, except for being somewhat lengthy. On another note, the vote for blocks in short closures has failed lately (https://wiki.php.net/rfc/auto-capture-closure). The message above also addresses the syntax ambiguity you mentioned. The {} syntax would be unambiguous in the most useful contexts (e.g. function parameters, match arms, arrow function bodies, rhs of binary operators, etc.). It is ambiguous in the general expression context due to expression statements (statements containing a single expression followed by `;`), where it's unclear (without lookahead) whether the `{` refers to a statement block or a block expression. Replacing all statement blocks with block expressions comes with the added difficulty of allowing to omit the `;` of block expressions in a expression statement. I remember there also being issues with the optimizer (related to https://www.npopov.com/2022/05/22/The-opcache-optimizer.html#liveness-range= -calculation). The details went over my head at the time. I'm interested in picking this back up at some point, at least for match ar= ms. Ilija