Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:117890
Return-Path: <arnaud.lb@gmail.com>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 88185 invoked from network); 9 Jun 2022 16:29:07 -0000
Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5)
  by pb1.pair.com with SMTP; 9 Jun 2022 16:29:07 -0000
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id 9050D18053F
	for <internals@lists.php.net>; Thu,  9 Jun 2022 11:15:24 -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_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: <arnaud.lb@gmail.com>
Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.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 <internals@lists.php.net>; Thu,  9 Jun 2022 11:15:24 -0700 (PDT)
Received: by mail-wm1-f52.google.com with SMTP id m39-20020a05600c3b2700b0039c511ebbacso25579wms.3
        for <internals@lists.php.net>; Thu, 09 Jun 2022 11:15:24 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20210112;
        h=from:to:cc:subject:date:message-id:in-reply-to:references
         :mime-version:content-transfer-encoding;
        bh=VLeRKvfOc8qqAaMCZCVXtP2+MBChkfACqOACvWNc1ro=;
        b=pLLONndBXgTezElxljVhxwlFCC+ZcU6a5tuBffcXlpQpyZpMO1LihRHONlae+W3Mzg
         0nxwQugwTKJnOC/Ci7fOBLmOEVNnZYbUAUHdMP5o4GU8ow0D9p74XlfxTF+XnOXSih9z
         YjxsNFV4F/UULzaYQw6tIJ6nODmUWSGuUQaO/hvUxgn+B2yF6znJfH5B67a7MHEJNwPO
         tv2pwbY1wNuIccYYJf401A3MXoRuIEdI2pxHcIerq3dpIsflk0FX247Jx8HEGCznOPjT
         zYggWM2VEl+yrVRg/ZtB21BB9Ywn6Yol8rpn/1k6FLz5Gju5w67lQ9rYtUNhjMPtYdQ0
         GKIw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20210112;
        h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
         :references:mime-version:content-transfer-encoding;
        bh=VLeRKvfOc8qqAaMCZCVXtP2+MBChkfACqOACvWNc1ro=;
        b=03lhvF80BWqJqd4Njg065v/4io2VxX0PCfUz+3Vj2aNHfLnjsdfizjcPZRn6HvxAAQ
         19DFMJQV4VC6CyIdCk3bphB3YFHhUjEYHL7xrkP93vHVM733tiPaoGVY47NdQ7V6NXfk
         RLOvnmROu5lAdpDKPRspGnUHGM0xs9HEukcyo0e45ts3wW0oBowS8VmvfEkw3yKNU4tt
         W2FzN4QP0N+4mW/S/guOuZ2p05IQqw3ef5O6awYqfNr0aIjoz/zsckRKqdvxiSVie3aR
         vTk9kLMyYVTOATkr+I400SKNJx+cASBQUDdMeDPfQXiE19mXPChEivBBoisvqpd+ikJu
         sUFQ==
X-Gm-Message-State: AOAM5302ySG8iq9C02f8NMgRXMpKz/S+rh1r0UiftS6xQIP18HgJijIB
	DciXaJOtMY1tYfNVhggPx1Y=
X-Google-Smtp-Source: ABdhPJxRe8xY/plSxRc2yVvCE+Si0TyXHs4QzLKhVDHwS0z774OJEYQ1rQEzTKDocccnogk3OzJJFw==
X-Received: by 2002:a1c:7206:0:b0:39c:4d16:683f with SMTP id n6-20020a1c7206000000b0039c4d16683fmr4662753wmc.197.1654798522922;
        Thu, 09 Jun 2022 11:15:22 -0700 (PDT)
Received: from arnaud-t490.localnet (2a01cb04054b5b00b39f4a3b48bcd353.ipv6.abo.wanadoo.fr. [2a01:cb04:54b:5b00:b39f:4a3b:48bc:d353])
        by smtp.gmail.com with ESMTPSA id j3-20020a5d4483000000b0020fcc655e4asm25278090wrq.5.2022.06.09.11.15.21
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 09 Jun 2022 11:15:22 -0700 (PDT)
To: Larry Garfield <larry@garfieldtech.com>, internals@lists.php.net
Cc: php internals <internals@lists.php.net>, Marco Pivetta <ocramius@gmail.com>
Date: Thu, 09 Jun 2022 20:15:20 +0200
Message-ID: <21891029.EfDdHjke4D@arnaud-t490>
In-Reply-To: <CADyq6s+G_2YyyTb-E5DFnvxTH6J=53py=HXRBs8zhT=_3ceozA@mail.gmail.com>
References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <CADyq6s+G_2YyyTb-E5DFnvxTH6J=53py=HXRBs8zhT=_3ceozA@mail.gmail.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="us-ascii"
Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3
From: arnaud.lb@gmail.com (Arnaud Le Blanc)

Hi,

On jeudi 9 juin 2022 18:46:53 CEST Marco Pivetta wrote: 
> ## nesting these functions within each other
> 
> What happens when/if we nest these functions? Take this minimal example:
> 
> ```php
> $a = 'hello world';
> 
> (fn () {
>     (fn () {
>         echo $a;
>     })();
> })();
> ```

Capture bubbles up. When an inner function uses a variable, the outer function 
in fact uses it too, so it's captured by both functions, by-value.

This example prints "hello world": The inner function captures $a from the 
outer function, which captures $a from its declaring scope.

This is equivalent to

```php
(function () use ($a) {
    (function () use ($a) {
        echo $a;
    })();
})();
```

> ## capturing `$this`
> 
> In the past (also present), I had to type `static fn () => ...` or `static
> function () { ...` all over the place, to avoid implicitly binding `$this`
> to a closure, causing hidden memory leaks.
> 
> Assuming following:
> 
>  * these new closures could capture `$this` automatically, once detected
>  * these new closures can optimize away unnecessary variables that aren't
> captured
> 
> Would that allow us to get rid of `static fn () {` declarations, when
> creating one of these closures in an instance method context?

It would be great to get rid of this, but ideally this would apply to Arrow 
Functions and Anonymous Functions as well. This could be a separate RFC.

--
Arnaud Le Blanc