Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125878 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 BAABE1A00BD for ; Wed, 30 Oct 2024 09:59:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1730282503; bh=sTQNoJLj5cbmWdDD/1307EwtcqWYfQI0RpYqiCbU+vc=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=lGt7rlk/8VHb4LaysLMI+M+ix4Km/Gs3dDvz+MHd9smljQcw3XwAK42/dXyIi2qu+ jak2LJx0TAcJyKQ8M+jY2SeogV9cVJdTlVDZiTWryXj0cR4Ne4pHvJeglRwQ3e8wwv KLmy1UhyWbCfeg1tYQU1c1YS2woRT8lBy2vycKnE21OAIdexB3O6Wh4DyJUY4C+G8E GeIAppJDQoRrK6LZFeS7AlF2t0fYTT8SiRWxV5lynO/7J/ft3RRa/YpJZ1cWN4RvMN aRPxRLxasHZjuG5wBakGDxbLoUIs+pY4QcbBT84ph+m21Fat5ouK/C+AVpVsoQBXVx t9U8VOEYYwDBw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1DDC718006F for ; Wed, 30 Oct 2024 10:01:43 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) (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 ; Wed, 30 Oct 2024 10:01:39 +0000 (UTC) Received: by mail-yb1-f181.google.com with SMTP id 3f1490d57ef6-e30d0d84d23so715824276.3 for ; Wed, 30 Oct 2024 02:59:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730282350; x=1730887150; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=NyFxQR6kjEjTFHQPuyE7BkDbv2MqVVkoUyDfA09pFMQ=; b=NfQR4+m4DMPz0LKyyGDRfsomNVrlsuhJTMSlR7sdQuouMQVFoLCQuULwaBUF6BdN7v Adpv06bUVEQLe8IEw2r3Y4h5sPTJjSzW5ZNhqkRGlfgeX8YEVjN7Bgtf+5Xxn1cHC2ui 2ua7NchRBQ79HTznxtSMAh15LV1vaO4EqzdVmiYn87I0bqUTv0imMDsIHeKxR+9bxPXR +4UmgWpHCGaa8RxoyH0vqOlEYq0K7rywgQN1G+m51JxhU2BJj6RCuCtOU3629Are1VKD +4FUqPcKTCyy1fi6+2sIDyBxpADK4MD7Mk3UwFIU7aht0Tq8qYJIFugYOvT+LJwH5XR/ yZbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730282350; x=1730887150; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NyFxQR6kjEjTFHQPuyE7BkDbv2MqVVkoUyDfA09pFMQ=; b=gysFR8vuLjeDxv5jqxXc3KyjFXGGb/6gOmAygufelXqoiTIOnCrvJxDxHLQzBeWCJA 8MYAM0PB+334GaRQRtkZe5Qf/gTT9g3k0TnG3MsqzTpWaw/OEa5wu/6rm2E14Oit2xSj 153/+6Dt4uaXW4p7pTADU1g4pcPAXtoPbTq9nd8yKlskn7QSMxhdZt3zDbTUJyRYH+bq u/fqi9tf/h7DkGA1GXva7OgnSO45rmWfV2ySujNscpg+KNjw2c3/tsey22BL+/yov1Dj vgHYXzqz7Z2sxHzp/EdolHS2KylA2ETaxbR78CGCQ3clgP0PW4nJhqfY4cNISmzq+REa BXfA== X-Forwarded-Encrypted: i=1; AJvYcCWJ6Gj2ycA89yLr0Hp8e3teXFHyIkUkOeRr9lhmctTGCPEyFDYmNsEoooYAST0Mr8wB/LAmqkaTvoU=@lists.php.net X-Gm-Message-State: AOJu0YxrEdZ8LDAPIVBknaPAM69hAmxs2k00ZKPqrxrPCC7h+s/WKryy T3vTFYvwuw3m8kFZO/57KSaHLbuRIcimhk28okZetnWJkO+yi0WOl/WD42yJNP6twD44jbMoR3M rC4i7kXGDGAnXSQC1yMdFdyY7V8o= X-Google-Smtp-Source: AGHT+IF1p7G5KO+o6+AX8xhfxuAD/r5R4181yzQcMIN6DKz+aZS9RIMBSfjxgPNj4wQ034hfUPfeDxayjXG0ogPySko= X-Received: by 2002:a05:6902:1a44:b0:e30:d266:9e0a with SMTP id 3f1490d57ef6-e30d2669fc8mr1904008276.26.1730282350382; Wed, 30 Oct 2024 02:59:10 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <15da4c13445d7e9c9d768c60c19768d4@bastelstu.be> <3b458165-406c-4b70-97bc-6e98d6c44c72@app.fastmail.com> <6f39dce9e6b0579baa51bc84cb8140b9@bastelstu.be> In-Reply-To: <6f39dce9e6b0579baa51bc84cb8140b9@bastelstu.be> Date: Wed, 30 Oct 2024 10:58:59 +0100 Message-ID: Subject: Re: [PHP-DEV] RFC: Support Closures in constant expressions To: =?UTF-8?Q?Tim_D=C3=BCsterhus?= Cc: Larry Garfield , php internals Content-Type: multipart/alternative; boundary="000000000000db1b470625aec0b5" From: michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?=) --000000000000db1b470625aec0b5 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi Tim =C5=9Br., 30 pa=C5=BA 2024 o 09:02 Tim D=C3=BCsterhus na= pisa=C5=82(a): > 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 > > need to forbid short-closures. I fully agree that capturing variables > > for such functions doesn't work. What I don't understand is why that > > precludes short-closures. 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, but I do not know what they are and the RFC > > doesn't say.) > > It would indeed require some special handling to disable the > auto-capturing in the code. This would be solvable of course, but > there's also semantic ambiguity, because users reasonably expect short > closures to perform auto-capturing: > > > $foo =3D 'foo'; > > const Closure =3D static fn (array $bar): array =3D> [$foo, $bar]; > Personally, I'd expect short-closures be available as well, did you though about maybe reusing `const` token instead of `static` to make the information visually available for users? I mean if PHP could parse `const Closure =3D const fn (array $bar): array = =3D> [$foo, $bar];` and the documentation tells that `const` modifier prevent auto-capture in case of short-closures but also in case of normal closures to prevent use of use() clause that would be visible instantly in the code. Consider this syntax as well:
Hi Tim

=C5=9Br., 30 pa=C5=BA 2024 o 09:02= =C2=A0Tim D=C3=BCsterhus <tim@bastel= stu.be> napisa=C5=82(a):
Hi

Am 2024-10-30 05:25, schrieb Larry Garfield:
> This seems like a good idea to me.=C2=A0 My only real question is why = we
> need to forbid short-closures.=C2=A0 I fully agree that capturing vari= ables
> for such functions doesn't work.=C2=A0 What I don't understand= is why that
> precludes short-closures.=C2=A0 Is it not possible to "just"= say "there's
> nothing to even capture in this context, don't try"?=C2=A0 (T= here may be
> technical reasons for that, but I do not know what they are and the RF= C
> doesn't say.)

It would indeed require some special handling to disable the
auto-capturing in the code. This would be solvable of course, but
there's also semantic ambiguity, because users reasonably expect short =
closures to perform auto-capturing:

=C2=A0 =C2=A0 =C2=A0<?php

=C2=A0 =C2=A0 =C2=A0$foo =3D 'foo';

=C2=A0 =C2=A0 =C2=A0const Closure =3D static fn (array $bar): array =3D>= [$foo, $bar];

Personally, I'd expe= ct short-closures be available as well, did you though about maybe reusing = `const` token instead of `static` to make the information visually availabl= e for users?

I mean if PHP could parse `const Clos= ure =3D const fn (array $bar): array =3D> [$foo, $bar];` and the documen= tation tells that `const` modifier prevent auto-capture in case of short-cl= osures but also in case of normal closures to prevent use of use() clause t= hat would be visible instantly in the code.

Consid= er this syntax as well:

<?php

$foo =3D '';

const Closure =3D con= st function (array $bar): array { return [$foo, $bar]; }

This way we could also forbid use of global-variables, use() clause = and maybe even `static $baz =3D 1` inside functions. So the closures better= match constant semantics. WDYT?=C2=A0

The engine = could create an instance of let's say `\ConstClosure` that would be a s= ubtype of `\Closure` so it still can be used as a normal closure but not th= e other way. Effectively this would make these closures pure-functions if a= m not wrong here.

Overall I like the idea.

Cheers,
Micha=C5=82 Marcin Brzuchalski
--000000000000db1b470625aec0b5--