Re: Draft RFC: foreach iteration of keys without values
| From: | John Bafford | Date: | Thu, 03 Sep 2020 23:22:08 +0000 |
| Subject: | Re: Draft RFC: foreach iteration of keys without values | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111838@lists.php.net to get a copy of this message | ||
Hi everyone,
Apologies for not including context and responding to 40 emails all at once, I’m not at a computer
(and won’t be for a few weeks).
Given the comments I read in the thread, I wanted to make some key points that I hope will clarify
my intent behind the proposal:
* The primary motivation behind this RFC is the ability to indicate intent when iterating over a
collection without caring about its values. Any performance benefits are strictly secondary.
* I explicitly chose void, rather than null, because in PHP, null is a value, and personally, I
found it exceedingly weird to write
foreach($arr as $key =>
something-that-is-normally-a-value-except-in-this-case). In fact, if I’m remembering
correctly, I actually waited until PHP 7.1 added void for this RFC specifically so I
wouldn’t have to invent new syntax.)
* If PHP had either convention or special handling for _ or $_ as a “ignore this” destination, I
wouldn’t have made the proposal. However, it doesn’t; _ can (apparently!) be a constant, and is
also a function, and $_ has no special handling (and I bet it’s actually used to contain values
that are used in at least one application).
* From my recollection, unless something has changed drastically since 7.0 and 8.x, the only
possible performance hit from the patch would be in the parser, as there is obviously some
additional logic. However, at runtime, it can only be faster, because in the presence of a
void foreach value target, the parser simply doesn’t emit the opcode for writing a
value there..
* I know it’s popular to say that foreach(array_keys()) is rare; however, _every_ PHP
application I’ve ever touched uses it at least once, and often multiple times. So, I’m equally
surprised to find that there are people who have never used it. (If I had to guess, it’s that I
make heavy use of the fact that arrays are ordered maps, and so, simply the keys and their order
_are_ often a significant and useful piece of information on their own, especially in arrays that
are used as mapping tables.)
I think that covers all the criticisms I saw, but if I missed anything, please let me know and
I’ll respond once I have a chance. After I get back home, I’ll update the RFC with the results
of this thread and bring it to a vote.
Thanks,
-John