Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125427 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 E840C1A00BD for ; Wed, 4 Sep 2024 20:46:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725482889; bh=lOPUJOh+9PJyYgVppzoWSy9xpLqY0tDRntpH6DqnZIk=; h=Date:From:To:Cc:In-Reply-To:References:Subject:From; b=Lb7IAZb1N78rQiRrRrsenEcrAVOqXTy13O67pBO7bHr63Y9FRmW7PDKVss1XpdxvP JGoj+IU8KdI2lrGWEv2JlcZKqSqYeBEuRZIKDTUgcLFtY8188oZ9fkWIP0FMUDZ8Hz lCQu+YOiSlocdiDBHOvtYQlpRhWcksMF/Wn58fx4bZ7wvaL2g3oG2QALLmfLUHTfjo hBk3/24nt4IUuSjbXg2clhP1ZKjSFyG14V7tijc68+pnv9vlc8F4rhfCZ7Izc8dNtP TkMw+lLT1XOQfvVuubt4X9apYtN++8nYPrKmGXdSsPctFfOrtN84gOhqbAzBGwaCOZ gfXk4QXZ27wZg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E178D180032 for ; Wed, 4 Sep 2024 20:48:08 +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,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout1-smtp.messagingengine.com (fout1-smtp.messagingengine.com [103.168.172.144]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 4 Sep 2024 20:48:08 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfout.phl.internal (Postfix) with ESMTP id 8611A138028E; Wed, 4 Sep 2024 16:46:10 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-03.internal (MEProxy); Wed, 04 Sep 2024 16:46:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; h=cc: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=fm2; t=1725482770; x= 1725569170; bh=EVV65g24fjiaAz3gkrcVz8K1tR+G5UunJeFi8mMAlrA=; b=L D6DrPxbCWhA1MASjXIrzgN7dLR6eboHhaYCI2SUy/ZanZ7E88uKCqG7bP+Ga4lJN 9X8ZEQA9g62IRDl6AGnueLA2qJn9e0hoHUewS3nvlPk6dpUIfEC46rG6EXoXJPXp TswMMHA4xjXRtjtOD2sqfMG8jkq1I/6+cPpAZltUKiHzq95Tb5R8CoLPamIwtdjn kiIBm6/I/fTkA/V/1gqIjbhbGQ2REKnW9E0CJL/XZYF7LgmxLeFheXWb3DAdWuc4 vvcdR6BK1M9Rc7cLv8DMbjCu0+F9wS1cp9G17DPTKa4fc1gFrYp6hFZPD+rjlu/B AJK4RnCAm1jJusOwlEu7g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; t=1725482770; x=1725569170; bh=EVV65g24fjiaAz3gkrcVz8K1tR+G 5UunJeFi8mMAlrA=; b=eQvVHzjl0OC1p9dxc1JD3EHZhr/NdqGAEo4YJllHTv+v WLyp8FP5xwU62ELEZ+uVyYOIuyUcNDc+wpW3rxtaixyLqGsYrs4DUYxeV6DAdxEm 1THxI3qvZG8W9XyiHNwnM7C7QKWHDfAAvP9XBWNDEqRPOnGlBy95c4AgCGn889PA 7hOcTTz5id88p8u2NzzNzLrIWa+B1kKFzNt4XVH8KhuhakI3XwAKmknX3Uut6BQC 8hnR+dtPlZbjyxhWgoYwAVu5qaHGOwJngD07LAInl6Arsi48lrpV1bnZLviq3m8S FWcQY9qSJrTTm0X1iAD/uqKw2MGq62dIp/z31ClfNQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrudehjedgudehvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefoggffhffvvefkjghfufgtsegrtderreertdej necuhfhrohhmpedftfhosgcunfgrnhguvghrshdfuceorhhosgessghothhtlhgvugdrtg houggvsheqnecuggftrfgrthhtvghrnhepieeuteehvddvfeejhffgieehleehhedthfef keejffelgfevvdekudetjeejtddtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomheprhhosgessghothhtlhgvugdrtghouggvshdpnhgspghrtghp thhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsh eslhhishhtshdrphhhphdrnhgvthdprhgtphhtthhopehjsggrfhhfohhrugesiihorhht rdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 3DD68780067; Wed, 4 Sep 2024 16:46:10 -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: Wed, 04 Sep 2024 22:45:48 +0200 To: "John Bafford" Cc: internals@lists.php.net Message-ID: <420bb5cb-5fca-4f2e-8c68-0ca327cd3392@app.fastmail.com> In-Reply-To: References: <27c3909f-05f4-4256-a447-10e8d8760fff@app.fastmail.com> Subject: Re: [PHP-DEV] Local constants Content-Type: multipart/alternative; boundary=a4c48f8a28254d4882bfa26035891a0f From: rob@bottled.codes ("Rob Landers") --a4c48f8a28254d4882bfa26035891a0f Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Wed, Sep 4, 2024, at 22:17, John Bafford wrote: > On Sep 4, 2024, at 15:23, Rob Landers wrote: > >=20 > > On Tue, Sep 3, 2024, at 05:20, HwapX wrote: > >> Hello internals! > >>=20 > >> I was wondering, has there been any discussion about supporting loc= al constants (variables that cannot be reassigned, perhaps even function= parameters)? > >=20 > > Out of curiosity, what value would this bring to PHP? In my experien= ce, modern php methods and functions tend to fit on a single screen, at = most being a few hundred lines for complex logic. > >=20 > > =E2=80=94 Rob >=20 > This is about semantic clarity. If you define a variable as a constant= , then, not only do you know for certain that it cannot change, you are = also stating "it is my intent to not change this variable after setting = it", and so if someone later tries to do so, they (should) have to answe= r the question, "*why* do I need to make this variable change?". >=20 > If it's useful to be able to annotate class members as being "readonly= ", it is likewise useful to do that on a local scope. >=20 > With sufficient compiler support, *typed* constant variable declaratio= ns might also allow for this: >=20 > let SomeType $foo; //or readonly, or writeonce, or whatever >=20 > if($something) { > $foo =3D ...; > } else if($somethingElse) { > $foo =3D getSomeString(); //Error, type mismatch; possibly catchable a= t compile time, definitely with static analysis > } else if($thirdCondition) { > $foom =3D ...; //oops, typo > } else { > throw new Exception(...) > } >=20 > doSomething($foo); //Compiler error: $foo not initialized on all call = paths. > } >=20 > You might also tighten scoping with such variables: >=20 > foreach(...) { > let $foo =3D //local working variable; possibly even shadowing a varia= ble in the parent scope > } >=20 > //$foo is undefined; you can't access the last-set value from the loop= outside of the loop >=20 > Static analysis can catch all these errors, but it'd be nicer to be ab= le to do it without requiring docblocks. The language providing tools fo= r being more explicit about intent in code is never bad. >=20 > -John I think, conceptually, this makes sense, but maybe I'm the only one who = writes $arr =3D doSomething(); $arr =3D array_map(fn($x) =3D> $x->prop, $arr); $arr =3D array_filter($arr, $filterFunc); instead of $arr =3D array_filter(array_map(fn($x) =3D> $x->prop, doSomething()), $f= ilterFunc); And I feel like having constant variables would result in more of the la= tter, which I feel is more unreadable. Though I do note that my opinion = of what is readable might be different from other people's. That being said, I would much rather see block-scoped variables than loc= al variables via let or var or something: var $aNumber =3D 12; foreach($arr as var $item) { echo $item; // item exists here } echo $item; // item doesn't exist here PHP is simply too verbose to really benefit from local constants, but wo= uld benefit from block-scope far more. For example, with local constants= , you couldn't write that foreach because that variable exists in the sc= ope of the function and can only be defined once. =E2=80=94 Rob --a4c48f8a28254d4882bfa26035891a0f Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Wed, Sep 4, = 2024, at 22:17, John Bafford wrote:
On Sep 4, 2024, at 15:23, Rob Landers <rob@bottled.codes> wrote:

> On Tue, Sep 3, 2024, at 05:20, Hwap= X wrote:
>> Hello internals!
>>&= nbsp;
>> I was wondering, has there been any discuss= ion about supporting local constants (variables that cannot be reassigne= d, perhaps even function parameters)?

=
> Out of curiosity, what value would this bring to PHP? In my ex= perience, modern php methods and functions tend to fit on a single scree= n, at most being a few hundred lines for complex logic.
&g= t; 
> =E2=80=94 Rob

T= his is about semantic clarity. If you define a variable as a constant, t= hen, not only do you know for certain that it cannot change, you are als= o stating "it is my intent to not change this variable after setting it"= , and so if someone later tries to do so, they (should) have to answer t= he question, "*why* do I need to make this variable change?".
<= div>
If it's useful to be able to annotate class members a= s being "readonly", it is likewise useful to do that on a local scope.

With sufficient compiler support, *typed* co= nstant variable declarations might also allow for this:
let SomeType $foo; //or readonly, or writeonce, or whatever=

if($something) {
$foo =3D ..= .;
} else if($somethingElse) {
$foo =3D getS= omeString(); //Error, type mismatch; possibly catchable at compile time,= definitely with static analysis
} else if($thirdCondition= ) {
$foom =3D ...; //oops, typo
} else {
=
throw new Exception(...)
}

doSomething($foo); //Compiler error: $foo not initialized on al= l call paths.
}

You might als= o tighten scoping with such variables:

fore= ach(...) {
let $foo =3D //local working variable; possibly= even shadowing a variable in the parent scope
}
=

//$foo is undefined; you can't access the last-set v= alue from the loop outside of the loop

Stat= ic analysis can catch all these errors, but it'd be nicer to be able to = do it without requiring docblocks. The language providing tools for bein= g more explicit about intent in code is never bad.

-John

I think, concept= ually, this makes sense, but maybe I'm the only one who writes
=

$arr =3D doSomething();
$arr =3D array_map= (fn($x) =3D> $x->prop, $arr);
$arr =3D array_filter(= $arr, $filterFunc);

instead of

$arr =3D array_filter(array_map(fn($x) =3D> $x->p= rop, doSomething()), $filterFunc);

And I fe= el like having constant variables would result in more of the latter, wh= ich I feel is more unreadable. Though I do note that my opinion of what = is readable might be different from other people's.

That being said, I would much rather see block-scoped variables= than local variables via let or var or something:

var $aNumber =3D 12;

foreach($arr as va= r $item) {
  echo $item; // item exists here
}

echo $item; // item doesn't exist h= ere

PHP is simply too verbose to really ben= efit from local constants, but would benefit from block-scope far more. = For example, with local constants, you couldn't write that foreach becau= se that variable exists in the scope of the function and can only be def= ined once.

=E2=80=94 Ro= b
--a4c48f8a28254d4882bfa26035891a0f--