Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120606 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 71259 invoked from network); 16 Jun 2023 19:23:24 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Jun 2023 19:23:24 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AA6B71804DF for ; Fri, 16 Jun 2023 12:23:23 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, 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-yb1-f194.google.com (mail-yb1-f194.google.com [209.85.219.194]) (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 ; Fri, 16 Jun 2023 12:23:23 -0700 (PDT) Received: by mail-yb1-f194.google.com with SMTP id 3f1490d57ef6-bd5f20508f2so996799276.3 for ; Fri, 16 Jun 2023 12:23:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20221208.gappssmtp.com; s=20221208; t=1686943402; x=1689535402; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=2wXRYJTfBwV1w5MNhEXRWU8oaEwKTbAL3dzbHYKNALI=; b=oQNjX7C7+HtTLlHvts4pLt+nBkqwYo/yVikMmlbVs9V8JMJrtafJRtWVAV34QCeOgS pj9kFKQ0vDUOuQeukFEpIdJpfKfWgyMW6a3nhk8x5uPP0k44Ym6j+ro2O6eI8OCBJuar Z4AS8IUw9knglkb3ppOdjr1EachU5neri0D391YMO/b8LZxqBw5YQqeRAuW1TVrDvuA1 oWkgLA0tn4LEgjBA8rS0UAWjgD6frr7PKyqjSkmgNJ+uZinsC6W8HJR/MSrIl0/+P+fI n0ASfMFLKl2ZDTjar21ZLoKR838pYi4fd/xXfOC4N0ot2XcgfDGIV9G0hVf/7IPn52I/ Gmig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686943402; x=1689535402; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=2wXRYJTfBwV1w5MNhEXRWU8oaEwKTbAL3dzbHYKNALI=; b=NMJht2DUQXuM3aCL/yBfYSS0Mea7yTkj11d826YLLtJ4H6FSX5OgFxwU8mJxanho5i XNfPptYaB5wAsi8lju/UzNDrLuqoNWEQamWdYTgFdGj/jiNWmKQ5RZnRF1pcu+ZxNm3R 2Gvo86v96PwTAsm30zgeLNFPZNkMLLt9tQGYVzXDIBG0bGmPyzNZVuLxtwxsKlTesc1r 050DEuBFIXoHKqi4zIEeR4Z9Hc18uBn6gEf43N5icAo+61FCX9VTcQ9nIWfOfc4E396b YtH8gTv0yqdzflpU4pGUVWeuDDcZtfyBvWinfmCm1kPnW2b+sT5Q3hWkCjiKMx9qxGOA gFLA== X-Gm-Message-State: AC+VfDzgWAh4dWCq3Gn4pCY2M9ztE3yT3TRZO0yo7FvKjYzZp0loaInK HfUGFwQEHwAS1+HGPPhSCQViuZqjjhbdcsFTIyKjL4t0A40IVrXnctgcoGd5 X-Google-Smtp-Source: ACHHUZ4Z25XpG4SkGNLtKBcsHAhy46/+Qg5n+Yy+7ykuRR1SWJc9E6yB7w2qS6AgkYkJVG8aCfJMLMDph1z6gTI4juc= X-Received: by 2002:a25:addb:0:b0:bcb:a8bf:6d57 with SMTP id d27-20020a25addb000000b00bcba8bf6d57mr40945ybe.21.1686943402420; Fri, 16 Jun 2023 12:23:22 -0700 (PDT) MIME-Version: 1.0 Date: Fri, 16 Jun 2023 21:23:11 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: [PHP-DEV] Expression code blocks From: andreas@dqxtech.net (Andreas Hennings) 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, like this: public function f(string $key) { return $this->cache[$key] ??= { // Calculate a value for $key. [...] return $value; } } Currently, to achieve the same, we have two options which both add overhead both in code verbosity and in performance: 1. Call another method or closure after the ??=. 2. Use if (isset(...)) instead of the ??= shortcut. This results in repetition of other parts of the expression, because the if/else cannot be inside the expression. E.g. this is option 1 with a closure: public function f(string $key) { return $this->cache[$key] ??= (function () use ($key) { // Calculate a value for $key. [...] return $value; })(); } Option 1 with a real method would look like this: public function f(string $key) { return $this->cache[$key] ??= $this->calc($key); } private function calc(string $key) { // Calculate a value for $key. [...] return $value; } The `{}` syntax seems like the most obvious choice at first, but I think it would not work. Statement groups with curly brackets are already possible right now, see https://www.php.net/manual/en/control-structures.intro.php, but they are not really useful for anything: They cannot be used as expressions, they don't have their own return value, and they don't isolate their variables. Another option would be a special keyword before the curly block. We could introduce a new keyword like `expr`, or use an existing one like `fn`. $x = 4 + fn {return 3;}; // or $x = 4 + expr {return 3;} The compiler/interpreter could either convert the block into a closure, or treat it as a new language feature, which might bring performance benefits. Any thoughts? -- Andreas