rfc:list_keys
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:list_keys [2016/01/17 02:39] – note open issues ajf | rfc:list_keys [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Allow specifying keys in list() ====== | ====== PHP RFC: Allow specifying keys in list() ====== | ||
- | * Version: 1.0 | + | * Version: 1.1.1 |
* Date: 2016-01-17 | * Date: 2016-01-17 | ||
* Author: Andrea Faulds, ajf@ajf.me | * Author: Andrea Faulds, ajf@ajf.me | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 75: | Line 75: | ||
</ | </ | ||
- | Keys can not only be literals like < | + | Keys can not only be literals like < |
<code php> | <code php> | ||
Line 84: | Line 84: | ||
) = $curlOptions; | ) = $curlOptions; | ||
</ | </ | ||
- | |||
- | However, keys cannot be variables or other expressions: | ||
<code php> | <code php> | ||
- | // Parse error: syntax error, ... | + | list($foo => $bar) = $bar; |
- | list($foo => $bar) = $array; | + | |
</ | </ | ||
- | |||
- | The problem here is that there are two different behaviours that you might expect here: | ||
- | |||
- | - < | ||
- | - < | ||
- | |||
- | So, this is not allowed in order to prevent misunderstanding. (However, this might change: see //Open Issues// below.) | ||
< | < | ||
Line 140: | Line 130: | ||
]; | ]; | ||
- | list(" | + | list(" |
</ | </ | ||
Line 159: | Line 149: | ||
Handling of implicit conversions of keys, and of accessing undefined keys, follows the same rules as for regular array indexing. | Handling of implicit conversions of keys, and of accessing undefined keys, follows the same rules as for regular array indexing. | ||
+ | |||
+ | ==== Further Examples ==== | ||
+ | |||
+ | The use of explicit integer keys can be clearer than regular un-keyed < | ||
+ | |||
+ | <code php> | ||
+ | $result = $dispatcher-> | ||
+ | switch ($result[0]) { | ||
+ | case \FastRoute\Dispatcher:: | ||
+ | list(, $handler, $parts) = $result; | ||
+ | | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code php> | ||
+ | $result = $dispatcher-> | ||
+ | switch ($result[0]) { | ||
+ | case \FastRoute\Dispatcher:: | ||
+ | list(1 => $handler, 2 => $parts) = $result; | ||
+ | | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The comma in the version using implicit keys could be missed when reading the code, and here we are mixing two different kinds of indexing: explicit (< | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 190: | Line 206: | ||
No impact on php.ini. | No impact on php.ini. | ||
+ | |||
+ | ===== Resolved Issues ===== | ||
+ | |||
+ | ==== Should arbitrary expressions be allowed as keys? ==== | ||
+ | |||
+ | Given this syntax, it was thought there might be potential confusion as to what < | ||
+ | |||
+ | <code php> | ||
+ | list($foo => $bar) = $array; | ||
+ | </ | ||
+ | |||
+ | The problem was that there were two different behaviours that might be expected here: | ||
+ | |||
+ | - < | ||
+ | - < | ||
+ | |||
+ | So, this was initially not allowed in order to prevent misunderstanding. However, after discussions and further thought, it seemed as though misinterpreting it as doing the first thing (taking the key of the first element) was unlikely, and so we do not need to restrict arbitrary expressions. It also is more useful to accept them, since this means things like object keys (e.g. with SplObjectStorage) can work. | ||
===== Open Issues ===== | ===== Open Issues ===== | ||
- | Due to the aforementioned potential confusion, arbitrary expressions (including variables) are not accepted as keys, merely constants. However, if we think that the two possibilities are unlikely to be confused, we could allow variable keys. This would be mostly useful for '' | + | None. |
===== Unaffected PHP Functionality ===== | ===== Unaffected PHP Functionality ===== | ||
Line 200: | Line 233: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
+ | |||
+ | __The following is merely **potential future scope** and **is not part of the proposal proper, nor being voted on in this RFC**.__ | ||
It would be useful to support < | It would be useful to support < | ||
Line 221: | Line 256: | ||
</ | </ | ||
- | This would be more practical to implement than the [[rfc:named_parameters|Named Parameters]] RFC, and could be applied to existing | + | This would be more practical to implement than the [[rfc:named_params|Named Parameters]] RFC, and could be applied to existing |
If we implemented this, we might want to extend the syntax to support type declarations: | If we implemented this, we might want to extend the syntax to support type declarations: | ||
Line 245: | Line 280: | ||
It has been suggested that at some point < | It has been suggested that at some point < | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
- | The vote will be a simple Yes/No vote on whether to accept the RFC and merge the patch into PHP master. | + | The vote is a simple Yes/No vote on whether to accept the RFC and merge the patch into PHP master. As this adds a feature to the language, this RFC requires a 2/3 majority to succeed. |
- | As this adds a feature to the language, this RFC requires a 2/3 majority to succeed. | + | Voting started on 2016-02-05 and ended on 2016-02-14. |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | I have written | + | php-src has a complete |
- | There is not yet a patch for the language specification. | + | There is a language specification |
===== Implementation ===== | ===== Implementation ===== | ||
+ | |||
+ | Merged into php-src for PHP 7.1: https:// | ||
+ | |||
+ | Merged into php-langspec for PHP 7.1: https:// | ||
+ | |||
After the project is implemented, | After the project is implemented, | ||
- | - the version(s) it was merged to | ||
- | - a link to the git commit(s) | ||
- a link to the PHP manual entry for the feature | - a link to the PHP manual entry for the feature | ||
===== References ===== | ===== References ===== | ||
- | * [[rfc:named_parameters|Named Parameters RFC]] | + | * [[rfc:named_params|Named Parameters RFC]] |
===== Rejected Features ===== | ===== Rejected Features ===== | ||
Line 272: | Line 315: | ||
===== Changelog ===== | ===== Changelog ===== | ||
+ | * v1.1.1 (2016-02-12) - Added further example | ||
+ | * v1.1 - Resolved issue of whether to permit arbitrary expressions in keys (yes) | ||
* v1.0 - First announced version | * v1.0 - First announced version |
rfc/list_keys.1452998397.txt.gz · Last modified: 2017/09/22 13:28 (external edit)