Newsgroups: php.internals
Path: news.php.net
Xref: news.php.net php.internals:117895
Return-Path: <nikita.ppv@gmail.com>
Delivered-To: mailing list internals@lists.php.net
Received: (qmail 98877 invoked from network); 9 Jun 2022 17:49:36 -0000
Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5)
  by pb1.pair.com with SMTP; 9 Jun 2022 17:49:36 -0000
Received: from php-smtp4.php.net (localhost [127.0.0.1])
	by php-smtp4.php.net (Postfix) with ESMTP id 0FDAC1804D4
	for <internals@lists.php.net>; Thu,  9 Jun 2022 12:35:55 -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,HTML_MESSAGE,
	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: <nikita.ppv@gmail.com>
Received: from mail-lf1-f43.google.com (mail-lf1-f43.google.com [209.85.167.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 <internals@lists.php.net>; Thu,  9 Jun 2022 12:35:54 -0700 (PDT)
Received: by mail-lf1-f43.google.com with SMTP id a29so11031372lfk.2
        for <internals@lists.php.net>; Thu, 09 Jun 2022 12:35:54 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20210112;
        h=mime-version:references:in-reply-to:from:date:message-id:subject:to
         :cc;
        bh=EiY0Mpw8z7emw2pvcA20ET/SUf4nVFrhrCxhQCnFnoU=;
        b=l86WH/O5eLaUFTvVUAJbqL3+dcElHrfWSzUsMuuBbg0siZl0+Czw5dEjSGy+ZD5kbH
         Yb4wneoh5NnGg2wKeNTmivf3bVUSpVV1KxBXUfoNh0HSuZGcs30q6oPzTdWDgNaNs2W/
         +V/hawUoDR8aExnUoCwTe9H6dhdmsPF07qnqPW2/VAhQVl25gegJ3n05QKXWnP2qJ5KQ
         yikFGsV00UARJIPlp1NDJ691M07NiNXqIwXr9Dohz7ceihg5U61tcvmEhr+GqAI2GWbH
         aZWHAwhNUJX0dBfha7C4VmKmKRXoUhCeAyeH/Clr5I6BHmR2qKi8Q5g9jE7tduAD68So
         xK7w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20210112;
        h=x-gm-message-state:mime-version:references:in-reply-to:from:date
         :message-id:subject:to:cc;
        bh=EiY0Mpw8z7emw2pvcA20ET/SUf4nVFrhrCxhQCnFnoU=;
        b=czH4a8CalDf8kHw1YDUutwLszpqyAqX3/VQleP8i/fXB7voSgY8xW/U/arHIX457oG
         RB4ZdcDR0fhPGyzaDXM+bja27lOq3fbJ8+qu/fePiq+370c74RircWhnnBklxyf/BS21
         puzlp1T45ynIRIe2+NuWgb3glZ8ONzIaAQPF9PGzJiJ54OuIOUpxScJRaWDg5rHnAtuC
         cx17Dm/PbdncuUwThDyVBPboSCiFeflyOle2/kppPf5FFOGtmIXq0/0wSuJGeZlD6V01
         Zw2XKeV34V6SswHD+YtuoXyyNLh6mzNgPihcLT7lP3AlSrdGnffKM8/iy/K9eJl32WDg
         nA+g==
X-Gm-Message-State: AOAM531TpGZX9sCxdCqtiRK8JEIhoPKQunS+VDak7/TW0779Ts9uLCWQ
	eAJ3hWJLIvnfW5aS/9ZQZVpJIM0XJzXRM6svN14=
X-Google-Smtp-Source: ABdhPJx0H104gpvkOlf9BLadNonlcOD/e4i3D4mYOCG7ih9Q1AZNERu0Lp+1xYu4FwTeNJWgDFrP3VN6PaCnQcxwuhc=
X-Received: by 2002:a05:6512:33cf:b0:478:ff22:edef with SMTP id
 d15-20020a05651233cf00b00478ff22edefmr25898569lfg.430.1654803353092; Thu, 09
 Jun 2022 12:35:53 -0700 (PDT)
MIME-Version: 1.0
References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com>
 <CADyq6s+G_2YyyTb-E5DFnvxTH6J=53py=HXRBs8zhT=_3ceozA@mail.gmail.com>
 <21891029.EfDdHjke4D@arnaud-t490> <CAF+90c8APp7NN4-seshvG2JZQWH-N=RgcU7NDHD0pnGPhiPOwQ@mail.gmail.com>
 <CADyq6sL45vdpfe=AiERohR_HvFV3DN4H0v9JJSjY3EvaaT4G9A@mail.gmail.com>
In-Reply-To: <CADyq6sL45vdpfe=AiERohR_HvFV3DN4H0v9JJSjY3EvaaT4G9A@mail.gmail.com>
Date: Thu, 9 Jun 2022 21:35:37 +0200
Message-ID: <CAF+90c8iOwLt-yWVdZRdVtZdwmY0PVV1gdjRj9At9jZ1hyccpQ@mail.gmail.com>
To: Marco Pivetta <ocramius@gmail.com>
Cc: Arnaud Le Blanc <arnaud.lb@gmail.com>, Larry Garfield <larry@garfieldtech.com>, 
	PHP Internals List <internals@lists.php.net>
Content-Type: multipart/alternative; boundary="00000000000008c5b505e108eecd"
Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3
From: nikita.ppv@gmail.com (Nikita Popov)

--00000000000008c5b505e108eecd
Content-Type: text/plain; charset="UTF-8"

On Thu, Jun 9, 2022 at 9:29 PM Marco Pivetta <ocramius@gmail.com> wrote:

>
> On Thu, 9 Jun 2022 at 21:27, Nikita Popov <nikita.ppv@gmail.com> wrote:
>
>> On Thu, Jun 9, 2022 at 8:15 PM Arnaud Le Blanc <arnaud.lb@gmail.com>
>> wrote:
>>
>>> 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.
>>>
>>
>> I've tried this in the past, and this is not possible due to implicit
>> $this uses. See
>> https://wiki.php.net/rfc/arrow_functions_v2#this_binding_and_static_arrow_functions
>> for a brief note on this. The tl;dr is that if your closure does "fn() =>
>> Foo::bar()" and Foo happens to be a parent of your current scope and bar()
>> a non-static method, then this performs a scoped instance call that
>> inherits $this. Not binding $this here would result in an Error exception,
>> but the compiler doesn't have any way to know that $this needs to be bound.
>>
>> Regards,
>> Nikita
>>
>
> Hey Nikita,
>
> Do you have another example? Calling instance methods statically is...
> well... deserving a hard crash :|
>

Maybe easier to understand if you replace Foo::bar() with parent::bar()?
That's the most common spelling for this type of call.

I agree that the syntax we use for this is unfortunate (because it is
syntactically indistinguishable from a static method call, which it is
*not*), but that's what we have right now, and we can hardly just stop
supporting it.

Regards,
Nikita

--00000000000008c5b505e108eecd--