Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118857 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 80271 invoked from network); 20 Oct 2022 08:52:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Oct 2022 08:52:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B731C1804A9 for ; Thu, 20 Oct 2022 01:51:59 -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,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-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (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 ; Thu, 20 Oct 2022 01:51:59 -0700 (PDT) Received: by mail-wr1-f44.google.com with SMTP id w18so33230412wro.7 for ; Thu, 20 Oct 2022 01:51:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=KUnDeyhGF03MSAhc98Mi70FC5ZJ1B5aimuL4nMxMatc=; b=FA30YHBN8piZEixW3RpWKlPeLz7eCSCuGl1LkrPCp2n4nr6/oH6ichooLv4taxt7eG zf2Wx59fe75p4Bf58S6hQmCmXk1wKb1U77MIBnUli72hXAniKAXVDxcAI5Ui7/J57DoZ aTtq/JSeI0NIEfTndCcrPMhTQSdjjcmFjQ5+uhJOVpYUVf3CA36jZEDmX+BeyfUbP6+5 3DzKNtFT1P0lYjnYjX9Wj0H1iZmi+iHKx1UhJGENLjpG0CajbkyWmr6I1S9yViHBPEnf R3n78OLx6W4zCP7EcuBYA39aBqH8vFLlSS3hkHXtOz+pJZNKUkcrv8YVVR5xCnzOaS9t 5Ncg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=KUnDeyhGF03MSAhc98Mi70FC5ZJ1B5aimuL4nMxMatc=; b=H7FOqwcJOHlxM7xxxY2fQkG2pMv61VPIdAmmVeUwAprkDKmzdBSrtgBCwA9uQbKWpQ 8ZiueSSVgpBAYyur9d1qysvpSqlaXTaGWyTL7UOnp9wcb4QSFafBvDXXJ1GC51VhD90B WT7OglcaCtMJ3oGWplr3IyewqP1+VEIhannTPk7YgynRpUc6iVGDbZGLkmCJs/xSsvUn eJaIFn3CZKgdcaETTuXc4dtVs3fvGjiPbaAsxX9A1Bvp9gG6+p9N1R6sQGD33/gSM5LP LyJHMrci9slOEt4ftTy1YfRzM7m9oWY7dYx+Sq6XBYKNl+0aJqZl/mpSvN6+shpZkPTn fhpA== X-Gm-Message-State: ACrzQf36NtFR3HiTF01nZyzfPNyzexoNAyLOacxylNURLvtK9a5p3PT1 dQwsK/OYF6AKavavTjVJ6VE49UUtsCY= X-Google-Smtp-Source: AMsMyM4ep6v4+jtWgBhaM5SSamaF2gEKCoU30PJz9gPNgkLteEab3RIk+nEiV0wt+ej1LLNthAMN3A== X-Received: by 2002:a05:6000:3cf:b0:231:6ed6:e978 with SMTP id b15-20020a05600003cf00b002316ed6e978mr7507135wrg.500.1666255917925; Thu, 20 Oct 2022 01:51:57 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id n9-20020a05600c3b8900b003b4ff30e566sm4986361wms.3.2022.10.20.01.51.56 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 20 Oct 2022 01:51:56 -0700 (PDT) Message-ID: Date: Thu, 20 Oct 2022 09:51:55 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.3.3 To: internals@lists.php.net References: Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Compact can't resolve outer scoped variables using short closures From: rowan.collins@gmail.com (Rowan Tommins) On 19/10/2022 18:04, David Rodrigues wrote: > It seems to me to be a reasonable problem and one that needs attention, as > the message is not that "compact cannot be used here", but that "the > variable does not exist". I'd just like to point out that the error message here is 100% correct: there is nothing wrong with using compact() inside a closure, it just only has access *at run-time* to the variables which were captured when the closure was created, which are determined *at compile-time*. $foo = fn() => [$a, compact('a', 'b')]; is essentially compiled to: $foo = function (use $a) { return [$a, compact('a', 'b')] }; When you later execute $foo, compact() can see the local $a, but there is no $b anywhere in the compiled definition. On 20/10/2022 08:15, Claude Pache wrote: > Although it is difficult to make it work in general (of course), there is the specific case of names given as literal strings, as in the example provided by the OP, that does not suffer from the impossibility of static analysis. While it would be possible for the compiler to special-case this scenario, I think it could be rather confusing to have a function that is sometimes evaluated at compile-time and sometimes at run-time (other than as a transparent performance optimisation). For instance, each of the following *can* be fully evaluated to a constant at compile-time, but which ones would be? compact('a' . 'b'); $name = 'a'; compact($name); $x = 1; compact('a' . $x); $x = 1; $x++; compact('a' . $x); I think it would be better to look at a new syntax for specifying such arrays that doesn't rely on a run-time function, similar to how in JSĀ  { foo, bar } is short-hand for { foo: $foo, bar: $bar } This has come up before, for instance in Nikita's discussion of named parameter syntaxes: https://wiki.php.net/rfc/named_params#shorthand_syntax_for_matching_parameter_and_variable_name As well as removing the need to pass a variable name as a string, this would allow combining explicit and automatic names in one literal, e.g.: // Current array literal $x = [ 'foo' => $foo, 'bar' => someFunc() ]; // compact() $bar = someFunc(); $x = compact('foo', 'bar'); // Shortest variation in Nikita's RFC linked above $x = [ :$foo, bar: someFunc() ]; I'm personally not a fan of this coding style, because I think variables should be named to be meaningful in the current scope, not somewhere they're coming from or going to, but a dedicated syntax would at least allow that flexibility. Regards, -- Rowan Tommins [IMSoP]