Re: [RFC] Auto-capture multi-line closures andshortfunctions take 2

From: Date: Thu, 25 Mar 2021 17:01:52 +0000
Subject: Re: [RFC] Auto-capture multi-line closures andshortfunctions take 2
References: 1 2 3 4 5 6 7 8 9 10 11 12 13  Groups: php.internals 
Request: Send a blank email to internals+get-113787@lists.php.net to get a copy of this message
On 25/03/2021 16:24, Nuno Maduro wrote:
Concerning the comments about what's exactly "auto-captured" by the scope of a multi-line short closure, we will be just reusing the "auto-capture" feature that already exists in one-line short closures. Therefore, this RFC doesn't have plans on changing the way "auto-capture" already works in PHP.
This may be technically true, but it's not particularly helpful. I'm not sure if the details are actually documented anywhere, and there's an awful lot of PHP code that either can't be used in a single-expression closure, or is extremely unlikely to be used. For instance, which of the following will capture $a? $assignedBeforeUse = fn() {
    $a = 42;
    echo $a;
} $unreachableUse = fn() {
    if(false) { echo $a; }
    $a = 42;
} $assignedBeforeUnreachableUse = fn() {
    $a = 42;
    if(false) { echo $a; }
} $unsetRatherThanWritten = fn() {
    unset($a);
    echo $a;
} $outerClosure = fn() {
    $nestedClosure = fn() {
        echo $a;
    }
} Regards, -- Rowan Tommins [IMSoP]

« previous php.internals (#113787) next »