Re: Local constants

From: Date: Wed, 04 Sep 2024 21:41:04 +0000
Subject: Re: Local constants
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-125431@lists.php.net to get a copy of this message
On Wed, Sep 4, 2024, at 23:11, John Bafford wrote: > > > On Sep 4, 2024, at 16:45, Rob Landers <rob@bottled.codes> wrote: > > > > I think, conceptually, this makes sense, but maybe I'm the only one who writes > > > > $arr = doSomething(); > > $arr = array_map(fn($x) => $x->prop, $arr); > > $arr = array_filter($arr, $filterFunc); > > > > instead of > > > > $arr = array_filter(array_map(fn($x) => $x->prop, doSomething()), $filterFunc); > > IMO, that's a failing of PHP not supporting object syntax (and reasonable api) for > non-object values. In other languages, I can write code such as: > > let arr = doSomething() > .map { $0.prop } > .filter(filterFunc) > > Which is more readable than both PHP versions. I think that sidesteps the reality we are currently in and is orthogonal to the actual problem I perceive. To be more clear, I think with the current APIs in PHP, having local constants would incentivize people to write less readable code for the reasons you mentioned. Could the APIs be improved? Probably, if not certainly, but that is not what is on the table. > > > And I feel like having constant variables would result in more of the latter, 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 local variables via > > let or var or something: > > > > var $aNumber = 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 would benefit from > > block-scope far more. For example, with local constants, you couldn't write that foreach > > because that variable exists in the scope of the function and can only be defined once. > > > > — Rob > > I'd suggest that the foreach could be written with either of let/var to both locally scope > $item, which would explicitly disallow/allow $item to be changed while in the loop. Maybe, but I only mention that block-scope would be more valuable than local constants. That they work well together is also interesting. > > Another benefit of local constants is preventing accidentally reusing a prior variable. For > example, if I set a temporary variable to something, and then later in the function, reuse that same > name for a new temporary but don't set the value on all flow paths, then the value from earlier > in the function leaks through, whereas either using a constant, or a second explicit variable > definition would have exposed and/or prevented the issue. > > -John This is a problem that I have run into in only a handful of times in my entire career, across all languages, lots of times in JavaScript files of yesteryear when we had to deal with 10k loc handrolled files. I’ve seen this happen maybe 2-3 times in php (where it has been a bug), and a couple of times in C. I don’t think I’ve ever run into it in C#, Scala, or Python. Maybe I’m just lucky, but I don’t feel like this is a valid reason for the feature. Const was added to JavaScript (according to my cobwebbed memories) to introduce block scoping and optimizations around that. In PHP, we also have function scope, like JavaScript, but we also have lexical scope as well. Javascript did not have that at the time; in other words, the following would have output "world": function test() { console.log(hello) var hello = 'world' } test(); Today, it doesn't, and neither would php. The problems that const/let set out to solve in Javascript do not apply here, IMHO. — Rob

« previous php.internals (#125431) next »