Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120611 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 37117 invoked from network); 17 Jun 2023 14:59:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Jun 2023 14:59:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A4DCF180382 for ; Sat, 17 Jun 2023 07:59:08 -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=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, 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-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (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 07:59:08 -0700 (PDT) Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-30fcda210cfso1277121f8f.3 for ; Sat, 17 Jun 2023 07:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687013946; x=1689605946; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=rgLORgiClnbDoJts9iAsgyiN+09D3KWCfA2YOQXTxHQ=; b=CbD0fz78SfZku2wEr7pCtBU2jd6VPfN77796EkwCMxmS0IqCO0Itfkp6xwO/+PWu9Q GZwS6J6Qao4d8+6oAkuxOrYOXsNjJyphpTZgHM8g3w0BMt+HGnGnuaUKh3m9bIxUGh3T w57MiZI+AfmokW9QsBSb7ZvpitF5oiJq8EpWkoxH9WzH9H+T9g6kq89Vmw3XljcV/2xo Ay2RJrXkIQopRI0Ay/Nm71qp60LN7ndQeQqy7TttFLz912WaAKzkPYqK3vgmOOxJ4+EZ C6xrOiyRHTALmTd1zILuVaauohcRr6aCnG7/xnS3Oza6L4yloYeGzAmY9tjQaQs8wY7x L2cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687013946; x=1689605946; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rgLORgiClnbDoJts9iAsgyiN+09D3KWCfA2YOQXTxHQ=; b=C7gxT/j5h6ra6OTmF8OrajVySmCGDw2bHsX8ZrjgnWb/YJK/LuqoI7zSr+THl0OxYh liVy0znmQkbiv01U0L3Ch68SuNr9wG3Tv9uFG+ryfix4KRq6xtD/9ynGj4u91rweZkQ2 J/Rz9lyTx6GAhsnGrpwrxVZx90Ym7q/wgypcfCBY+1aEz9CqyVXdVH8fThQng5IFDQTt fga1LOkTZnmrtQgq4pbgwUhvdSjbajNvrzH1slhxfdomjfboHjhLDHfalMuJZvz3nL/+ MDcirV4Of+e7sech/jdAKF5W7TizAg+AU4apQcv6SFS2QuTxzvCathNZnXlymtIcWcS9 6tDg== X-Gm-Message-State: AC+VfDxvujicKdIwc0DLLjCW0YWu8jWRXzAPucGgvHPFTfe1z5uAxghT Peq2KHYc5ZkacwM5FwwNFc7gX6G9j0g= X-Google-Smtp-Source: ACHHUZ6+pIwFR3ptBOV9CpvNJHXmkAECOHLtB4mBtrd28V9Avsp92KD4HPOLcW4PTSSILaElj00vfA== X-Received: by 2002:adf:f241:0:b0:311:184a:6725 with SMTP id b1-20020adff241000000b00311184a6725mr5119127wrp.44.1687013946536; Sat, 17 Jun 2023 07:59:06 -0700 (PDT) Received: from [192.168.0.22] (cpc83311-brig21-2-0-cust191.3-3.cable.virginm.net. [86.20.40.192]) by smtp.googlemail.com with ESMTPSA id z18-20020a5d44d2000000b003110dc7f408sm9571340wrr.41.2023.06.17.07.59.05 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Jun 2023 07:59:05 -0700 (PDT) Message-ID: Date: Sat, 17 Jun 2023 15:59:04 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Content-Language: en-GB To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Expression code blocks From: rowan.collins@gmail.com (Rowan Tommins) On 17/06/2023 12:26, Ilija Tovilo wrote: > 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. I've been pondering various things in this space for a while, particularly since the last auto-capture closures RFC. I haven't quite coalesced on a coherent concept, but there are a few things that I think inter-relate: * Opting into block-scoped variables, while retaining full access to the outer scope, like JS "let" * Code blocks as some kind of first-class thing, distinct from closures * Macro-like constructs that take a block of code and do something special with it, or something like Python's "context managers" One of the use cases people often cite for auto-capture in closures is for use in patterns like this: $db->doInTransaction(function() use (...) {     $db->execute('some SQL here');     $db->execute('some more SQL');     // etc }); Or this: $name = $cache->memoize(function() use (...) {     $result = $db->query('some SQL');     return $result['item_name']; }); This is a nice pattern, but closures are quite a heavy tool for this job, requiring two extra stack frames (the doInTransaction or memoize method, and the closure itself), and boilerplate to pass the result back through. As I say, I haven't got a coherent design, but I think it would be great to have something context-manager-like: $name = with($cache->memoize() as $cacheItem) {     let $result = $db->query('some SQL');     $cacheItem->save( $result['item_name'] ); } ); ... or macro-based: $name = null; $cache->memoize!($name, block {     let $result = $db->query('some SQL');     $name = $result['item_name']; } ); Regards, -- Rowan Tommins [IMSoP]