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.tak...@gmail.com> 
Sent: Thursday, September 3, 2020 3:31 AM
To: Nikita Popov <nikita....@gmail.com>; Mike Schinkel <m...@newclarity.net>
Cc: John Bafford <jbaff...@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

 

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to