Req #79285 [Com]: Allow unpacking of arrays with string keys into other arrays
| From: | caelan89 at gmail dot com | Date: | Wed, 19 Feb 2020 21:23:29 +0000 |
| Subject: | Req #79285 [Com]: Allow unpacking of arrays with string keys into other arrays | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225634@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
Comment by: caelan89 at gmail dot com
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: N
Private report: N
New Comment:
Sorry, not array_match, array_merge.
I'm tired.
Previous Comments:
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
[2020-02-19 15:13:25] caelan89 at gmail dot com
Description:
------------
You've added in array unpacking inside of arrays.
However, it is a half-baked solution (if you can even call it that), and relatively useless except
for a few use cases.
You do not allow this:
$a1 = [
'test' => 1
];
$a2 = [
'test-2' => 2
];
$a3 = [
...$a1,
...$a2
];
There is absolutely zero reason, other than your inconvenience building it, as to why this should be
forbidden. None at all.
It is semantically equivalent to:
$a3 = array_merge($a1, $a2);
Why on earth should I not be able to do the same with with the much neater and preferable array
unpacking syntax?
Test script:
---------------
$a1 = [
'test' => 1
];
$a2 = [
'test-2' => 2
];
$a3 = [
...$a1,
...$a2
];
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79285&edit=1