Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117893 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 95721 invoked from network); 9 Jun 2022 17:41:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 9 Jun 2022 17:41:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3A4F71804F8 for ; Thu, 9 Jun 2022 12:27:32 -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: Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (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, 9 Jun 2022 12:27:31 -0700 (PDT) Received: by mail-lj1-f180.google.com with SMTP id m25so23966170lji.11 for ; Thu, 09 Jun 2022 12:27:31 -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=yV1sdj3S4OWiCIrC42I0wz+AwrcH3lTGAsjcNWAgUEE=; b=GZkmF5K3tXnx5+al7i6YR7X1mKm1gUKliKW2XkWak+cXgrIq0b4+pyr4FJInGvFvcd ftHqhsjsKiN+m5ldV9M1tjEOwsWbMzOv2HATfsO9duouLeetHGMtvDNbdJdx0VCP9uYO NH72Iv4Ksxl6lAm4Wy0L3risDCAugJ0zPjSq3NK8t1P+vsMZfWT8V54+ulrfZLSS9uZ0 wskAVba05u7V5GhdgE5Jm7HrPXw1Zh4kS1zM9DrpUjbxTTBXCUkeTvL4n/wA9FeuT+z8 5NIufW6UNz57lVGRW3Zo4uFU2ya4WAqMvScpANxmDamCInz5qDzXqqHge12yBOn/QqTE OP1g== 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=yV1sdj3S4OWiCIrC42I0wz+AwrcH3lTGAsjcNWAgUEE=; b=BTi+k1mTqMeUxJWntEjF6GTdA2bCZguv2rkc/iMEZleekCHIij4lXTYg+KMqcTlQMg zYAP241mFYXgVjuV3cN9Fx2eqpm32MZejZfb/kFOsVOS2dqUIaQjXiUvGYMzDwGRVKAe GDaPNkK58+MwjnzjEDMRJo1v/dpdd9VamfjaCj04aYZ/xbHSXOZawfmsElXVStZwERHY CHmxYiPqSkgQmLYXAMbN66+mrKPhdnDKZeP3AJX/ggrroUe6w0SYNi9dp8KqvH4PBTjU zMUOlyt6QZoCxaDRP9uOnmrO5xF9xkWvGGH6d4B7l74F11RvHDDNJ8cBCrRDrS7tWdW9 SWaw== X-Gm-Message-State: AOAM531cm9n7YZk6PogOXKh7i9b9Cwk1ndHy4qEuGMz8EkY06ZPb1ZOD KXvAxjFJjlQ8gWDrVICcSigQJ9sTLw0+Qj644lU= X-Google-Smtp-Source: ABdhPJx/WnV6Rx1RY274UAxg5EAnS98Ll8M3sBUxOiKtdmtFvxTPweDK0unVEdMiAViDg0Qxmj4QeGA0ZLHancvFZ1E= X-Received: by 2002:a2e:980c:0:b0:255:9589:4755 with SMTP id a12-20020a2e980c000000b0025595894755mr12838994ljj.353.1654802850207; Thu, 09 Jun 2022 12:27:30 -0700 (PDT) MIME-Version: 1.0 References: <2b35605f-8da8-46b1-aec3-00bd1bfe47fd@www.fastmail.com> <21891029.EfDdHjke4D@arnaud-t490> In-Reply-To: <21891029.EfDdHjke4D@arnaud-t490> Date: Thu, 9 Jun 2022 21:27:14 +0200 Message-ID: To: Arnaud Le Blanc Cc: Larry Garfield , internals@lists.php.net, Marco Pivetta Content-Type: multipart/alternative; boundary="0000000000000f5bd705e108d00b" Subject: Re: [PHP-DEV] [RFC] Short Closures 2, aka auto-capture take 3 From: nikita.ppv@gmail.com (Nikita Popov) --0000000000000f5bd705e108d00b Content-Type: text/plain; charset="UTF-8" On Thu, Jun 9, 2022 at 8:15 PM Arnaud Le Blanc 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 --0000000000000f5bd705e108d00b--