Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125883 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 8E2861A00BD for ; Thu, 31 Oct 2024 07:24:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1730359619; bh=FCJM5MvrGCIAgmRKmlmNKxhF4Qmq6BzM0MSW6N1R7L8=; h=Date:From:To:In-Reply-To:References:Subject:From; b=YUDVIlL4eM97BeJKtPe41J+PeI2v4u5g+Q87JAyz9LsnGCujgG+yv9VsVw+QDOQ9C acwoNBIlcLwgzZqHpXCqRJhuj+mKxJy9B6lAhMcg4eDSjTN7VfoYj3vv/ZEQmPxmW1 tYueQW4EoTUZTiDGp/kULlXDLnRVCIe1MRmIQC/U/Npel76Q1f5eWNfifmlCfzpcKj f2LuvTTsYOaYMRo39z5FAubzLs/HO4MdE20O5nnGym3oX4sS8R4FNi47JrskYwtmpp 8gVayokf7bsoCazhFUJLEhOIY1U4EbLSNIP1aBJvjIH/fK6eaBe58IA1krJyNtDfwR o6QTggB1aykxA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 48C9B18003F for ; Thu, 31 Oct 2024 07:26:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-a4-smtp.messagingengine.com (fout-a4-smtp.messagingengine.com [103.168.172.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 31 Oct 2024 07:26:57 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfout.phl.internal (Postfix) with ESMTP id 9603F138020F for ; Thu, 31 Oct 2024 03:24:28 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-01.internal (MEProxy); Thu, 31 Oct 2024 03:24:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1730359468; x=1730445868; bh=h/i4lZbYfX 2b2wgFf1jIA2jZ94bderIQHFsVnj6PiVs=; b=Km58FuC7NXaMkUuJCMJrFv2eRI L3vGcnzFteQtZ90Z2szjWQQzCy3FXPqJckG7N3miA6xPqWgpekdwyQQ7e6+xj/nA NxOmccptXcu1ZFCBUhFTefnslCcr+RmUMW1wn1eToecMY7qW3sDry/RrQw84KWZ5 ZYpkqEIBJWpTl9bjfnHbmXvN2U1QY+YNc+rn7W7Qar+NBca02TXAdYj1LY4sst8H kHKCCCNGjblYR2gNt6Yt9f9OEluxElNgtkCbcPcN3Y/tQCrLsY6Oq7yQgNIpgcml bTZbuTl7lYxV+2vrc8ceGSyYukxUaM5MiCYH+odx52mgzqFxP0v4hi/ck1Gw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1730359468; x=1730445868; bh=h/i4lZbYfX2b2wgFf1jIA2jZ94bderIQHFs Vnj6PiVs=; b=FLhd1XuwaoCJDudubB3f7lwvBO6sLOkw4LIzBd5L+u11EJrpOpa 3a08ER2FtUTqsCFCIkkd5EEo2l/NwgjINsLGNB9kmRIYzSwwmJY6EjJHW2egA/b/ MM9AyZxOEp6YXxyd3U1NKd1rx61ruIJVWEKA9/xLXunX4orAea6qafGK4SUdRQTV KH7he6Vlt/OEZXI2PcWQzADcDnChgJgbQT2IathEwMs8u2/HpuPFyUDFN+3V4eQa cuuPxxB4AtXSA1atdNjnRwmpFbE3SSeYEZyWZsuZwsgz8m9zjk/RZX7zUK4km1dQ K+hNJ4aQXnXcJRnL122a66inpKI2QhmAuKg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdekgedguddtiecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecuogfuuhhsphgvtghtff homhgrihhnucdlgeelmdenucfjughrpefoggffhffvkfgjfhfutgesrgdtreerredtjeen ucfhrhhomhepfdftohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtoh guvghsqeenucggtffrrghtthgvrhhnpedtiedtvddvvefhudffhfegleffteegffevkeeh keefleeuuddtieevkedvteejvdenucffohhmrghinhepfehvgehlrdhorhhgnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprhhosgessghothht lhgvugdrtghouggvshdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpd hrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 49F8A780068; Thu, 31 Oct 2024 03:24:28 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 31 Oct 2024 08:22:23 +0100 To: internals@lists.php.net Message-ID: In-Reply-To: References: <15da4c13445d7e9c9d768c60c19768d4@bastelstu.be> <3b458165-406c-4b70-97bc-6e98d6c44c72@app.fastmail.com> <6f39dce9e6b0579baa51bc84cb8140b9@bastelstu.be> Subject: Re: [PHP-DEV] RFC: Support Closures in constant expressions Content-Type: multipart/alternative; boundary=fc10d1b21f6344c2b1210220d5e6262c From: rob@bottled.codes ("Rob Landers") --fc10d1b21f6344c2b1210220d5e6262c Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Thu, Oct 31, 2024, at 07:16, Larry Garfield wrote: > On Wed, Oct 30, 2024, at 3:01 AM, Tim D=C3=BCsterhus wrote: > > Hi > > > > Am 2024-10-30 05:25, schrieb Larry Garfield: > >> This seems like a good idea to me. My only real question is why we=20 > >> need to forbid short-closures. I fully agree that capturing variab= les=20 > >> for such functions doesn't work. What I don't understand is why th= at=20 > >> precludes short-closures. Is it not possible to "just" say "there'= s=20 > >> nothing to even capture in this context, don't try"? (There may be=20 > >> technical reasons for that, but I do not know what they are and the= RFC=20 > >> doesn't say.) > > > > It would indeed require some special handling to disable the=20 > > auto-capturing in the code. This would be solvable of course, but=20 > > there's also semantic ambiguity, because users reasonably expect sho= rt=20 > > closures to perform auto-capturing: > > > > > > > $foo =3D 'foo'; > > > > const Closure =3D static fn (array $bar): array =3D> [$foo, $ba= r]; > > > > var_dump((Closure)('bar')); > > > > If this would be legal syntax: What would you expect to be printed? > > > > Best regards > > Tim D=C3=BCsterhus >=20 > Hm. It would never occur to me to use a function for a non-class cons= tant in the first place, so I don't know. :-) Frankly I can't recall th= e last time I used a non-class constant period. :-) >=20 > That said, PHP consts are already a bit squishy, and auto-capture is a= lways by value. That wouldn't give us a loophole? >=20 > If it cannot reasonably be done now, but is possible, that should be l= isted in future scope with roughly what it would take for a follow-up to= do. (And then we can argue if it should just be done now, with more in= formation as to how much work that is.) >=20 > --Larry Garfield >=20 To be honest, I thought it was a rhetorical question since the example i= s a runtime error (passing string to a function that takes an array), bu= t on this note, we already know how it should behave: https://3v4l.org/R= C6b3 because, as you said, it is captured by value. In theory, to suppor= t this, we would probably need =E2=80=9Clate binding constants=E2=80=9D = or constants that are executed just before any userland code is run. Sim= ilar to just emulating it yourself: https://3v4l.org/PMY4W IMHO, the rules around constant expressions are not sound and feel arbit= rary when you run into them. They are too easy to work around (though cu= mbersome) and the code still works, which seems to prove their arbitrary= nature.=20 =E2=80=94 Rob --fc10d1b21f6344c2b1210220d5e6262c Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable

=
On Thu, Oct 31, 2024, at 07:16, Larry Garfield wrote:
=
On Wed, Oct 30= , 2024, at 3:01 AM, Tim D=C3=BCsterhus wrote:
> Hi
<= /div>
>
> Am 2024-10-30 05:25, schrieb Larry Gar= field:
>> This seems like a good idea to me.  M= y only real question is why we 
>> need to forb= id short-closures.  I fully agree that capturing variables 
>> for such functions doesn't work.  What I don't= understand is why that 
>> precludes short-clo= sures.  Is it not possible to "just" say "there's 
>> nothing to even capture in this context, don't try"?  (= There may be 
>> technical reasons for that, bu= t I do not know what they are and the RFC 
>> d= oesn't say.)
>
> It would indeed requi= re some special handling to disable the 
> auto-ca= pturing in the code. This would be solvable of course, but 
> there's also semantic ambiguity, because users reasonably ex= pect short 
> closures to perform auto-capturing:<= br>
>
>      <= ?php
>
>     = $foo =3D 'foo';
>
>   =    const Closure =3D static fn (array $bar): array =3D> [$f= oo, $bar];
>
>    =   var_dump((Closure)('bar'));
>
>= If this would be legal syntax: What would you expect to be printed?
=
>
> Best regards
> Tim D= =C3=BCsterhus

Hm.  It would never occu= r to me to use a function for a non-class constant in the first place, s= o I don't know. :-)  Frankly I can't recall the last time I used a = non-class constant period. :-)

That said, P= HP consts are already a bit squishy, and auto-capture is always by value= .  That wouldn't give us a loophole?

I= f it cannot reasonably be done now, but is possible, that should be list= ed in future scope with roughly what it would take for a follow-up to do= .  (And then we can argue if it should just be done now, with more = information as to how much work that is.)

-= -Larry Garfield


To be honest, I thought it was a rhetorical question since the example = is a runtime error (passing string to a function that takes an array), b= ut on this note, we already know how it should behave: https://3v4l.org/RC6b3 because, as you s= aid, it is captured by value. In theory, to support this, we would proba= bly need =E2=80=9Clate binding constants=E2=80=9D or constants that are = executed just before any userland code is run. Similar to just emulating= it yourself: https://3v4l.org/P= MY4W

IMHO, the rules around constant ex= pressions are not sound and feel arbitrary when you run into them. They = are too easy to work around (though cumbersome) and the code still works= , which seems to prove their arbitrary nature. 

=E2=80=94 Rob
--fc10d1b21f6344c2b1210220d5e6262c--