RE: [PHP-DEV] Draft RFC: foreach iteration of keys without values
| From: | CHU Zhaowei | Date: | Thu, 03 Sep 2020 12:14:19 +0000 |
| Subject: | RE: [PHP-DEV] Draft RFC: foreach iteration of keys without values | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111813@lists.php.net to get a copy of this message | ||
Regarding the unused variable, I think OPCache should be able to identify and eliminate it. I
don't think it's necessary to add a new syntax here.
Array destructuring sounds like a similar case to me. If you don't like [, , $v] =$arr, you can
use [$_, $_, $v]=$arr instead.
Regards,
CHU Zhaowei
-----Original Message-----
From: Dik Takken <dik.takken@gmail.com>
Sent: Thursday, September 3, 2020 3:31 AM
To: Nikita Popov <nikita.ppv@gmail.com>; Mike Schinkel <mike@newclarity.net>
Cc: John Bafford <jbafford@zort.net>; PHP internals <internals@lists.php.net>
Subject: Re: [PHP-DEV] Draft RFC: foreach iteration of keys without values
On 02-09-2020 19:13, Nikita Popov wrote:
> Just like the first time this was discussed, I don't think this RFC
> makes a sufficient case on why we need explicit syntax for this. Just
> ignoring the value is an existing pattern that works across all PHP versions:
>
> foreach ($iterable as $key => $_) { ... }
While this works fine, it does introduce an unused variable. Code inspection currently complains
about that, and for good reason, which is annoying.
Even if we decide that it is not worth the trouble for foreach loops, there are other cases where
this new syntax can work well. Array destructuring has already been mentioned.
Another case is the one where a function intentionally does not use one of the arguments passed to
it. This happens easily when using callbacks or when implementing interfaces. A function declaration
could look like
this:
function foo($arg, void) {}
This indicates that the second parameter is intentionally left unused.
Perhaps it is a good idea to generalize the RFC to the general concept of "using void to
intentionally ignore a variable". Maybe pick just one use case for actual implementation and
extend later using followup RFCs.
Regards,
Dik Takken
--
PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php