Re: Local constants
| From: | Rowan Tommins [IMSoP] | Date: | Thu, 05 Sep 2024 21:44:30 +0000 |
| Subject: | Re: Local constants | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125446@lists.php.net to get a copy of this message | ||
On 5 September 2024 16:53:32 BST, Mike Schinkel <mike@newclarity.net> wrote:
>
>Not convinced? At least one other person has my same view of the dark despair of block-scoping:
>
>https://forum.golangbridge.org/t/warning-for-accidental-variable-shadowing-with-block-scope/4715
>
>Of all the Go code I have written, I'd say accidental variable shadowing is in the
>top-three (3) reasons for bugs in my code, if not the main reason. And if it were not for BC breaks,
>I would lobby hard for Go to remove it. I know that some on the Go team lament that they ever
>included it.
Block-scoping doesn't have to allow shadowing of names, and it certainly doesn't have to
be achieved by that incredibly hard to spot syntax. In most languages I've seen, it's
indicated by either a keyword such as "let", "var", or "my"; or by
indicating the type of the variable you're declaring.
It's something I'd really like to see in PHP. For one thing, it would make me much more
likely to accept the repeated requests for closures which capture parent scope by default, because
it would give a clear opt-out to replace the current clear opt-in of the use() clause.
I've never found myself wishing for local constants, but never really used a language with
them, so maybe I'd find them valuable if they were added.
For completeness, I'll mention that *typed* local variables are another related concept, which
has also come up in the past.
The big challenge in all of them is that these are big fundamental language changes, which require
an expert knowledge of how the language works under the hood to get much beyond "that would be
nice to have". Let's not get too deep into discussing the colour of the bikeshed before
we've worked out if there's space to build one.
Regards,
Rowan Tommins
[IMSoP]