Req #79285 [Nab]: Allow unpacking of arrays with string keys into other arrays
| From: | nikic@php.net | Date: | Wed, 19 Feb 2020 21:28:09 +0000 |
| Subject: | Req #79285 [Nab]: Allow unpacking of arrays with string keys into other arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225635@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79285&edit=1
ID: 79285
Updated by: nikic@php.net
Reported by: caelan89 at gmail dot com
Summary: Allow unpacking of arrays with string keys into
other arrays
Status: Not a bug
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: n/a
PHP Version: 7.4.2
-Block user comment: No
+Block user comment: Yes
Private report: N
New Comment:
Pro tip: Don't be a total dick in your bug reports, and you might get a more information reply
next time.
Previous Comments:
------------------------------------------------------------------------
[2020-02-19 21:23:29] caelan89 at gmail dot com
Sorry, not array_match, array_merge.
I'm tired.
------------------------------------------------------------------------
[2020-02-19 21:21:13] caelan89 at gmail dot com
Are there any downsides to allowing string keys when unpacking inside arrays?
I agree that allowing string keys for function arguments doesn't seem to semantically quite fit
together. Indexes don't map to arbitrary strings.
But why not for spreading within an array?
You could then simultaneously define default values, user customisable values and fixed values all
within the array syntax.
For example:
$custom = [
'value' => true,
];
$values = [
'value' => false,
...$custom
'fixed-value' => $whatever
];
Indexed arrays would concatenate, like with array_merge.
String keys will overwrite, like with array_merge.
Does it not make far more sense for the semantics to match array_match, which, if you did not know
PHP didn't support it, you would think it would?
------------------------------------------------------------------------
[2020-02-19 21:10:13] requinix@php.net
Yes, you are absolutely right, the two actually are different things. Perhaps that would explain why
the RFC used the words "consistent with" and not "is the same as".
------------------------------------------------------------------------
[2020-02-19 20:53:54] caelan89 at gmail dot com
requinx,
Unpacking an array to pass arguments to a function dynamically is not the same thing as unpacking an
array inside an array to merge in-place.
------------------------------------------------------------------------
[2020-02-19 18:57:15] requinix@php.net
https://wiki.php.net/rfc/spread_operator_for_array#string_keys
> In order to make the behavior consistent with argument unpacking, string keys are not
> supported.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=79285
--
Edit this bug report at https://bugs.php.net/bug.php?id=79285&edit=1