Re: Array spread append
| From: | Ilija Tovilo | Date: | Thu, 06 Apr 2023 10:18:50 +0000 |
| Subject: | Re: Array spread append | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119823@lists.php.net to get a copy of this message | ||
Hi Michael
> I would like to open a discussion for
> https://github.com/php/php-src/issues/10791 .
>
> [https://opengraph.githubassets.com/a23cb565cc8acac6a33ecab5d9ee68a46f046a1ffe215501673156e506695430/php/php-src/issues/10791]<https://github.com/php/php-src/issues/10791>
> Array spread append · Issue #10791 ·
> php/php-src<https://github..com/php/php-src/issues/10791>
> Description Currently spread operator can be used for almost anything. But not for array
> append. I propose the following to be supported: <?php $arr = [1, 2]; $arr2 = [3, 4]; $arr[...] =
> $arr2; // ...
> github.com
> Appending N elements to an array is quite common language usage pattern and I belive it should
> be supported natively for shorter syntax, language consistency and performance.Hi Michael
There are a few questions that come to mind (there may be more).
* Are integer keys preserved? I'm assuming no, as otherwise it would
be the same as
$a + $b.
* What is the return value of the expression $a[...] = $b? I'm
assuming $a after the additions of $b?
* How does it behave in combination with ArrayAccess? Throw? Call
offsetSet for each element?
* How does it interact with references? E.g. This is valid PHP code:
assign_by_ref($a[]) (https://3v4l.org/qoJYn)
* How does it interact with undefined/null values? E.g. $a[] = 42;
works without declaring $a first.
* Is there a need for this? Given that + doesn't work with
sequential lists and array_push($a, ...$b) doesn't work with strings
I'd say possibly. [...$a, ...$b] works but requires duplication of
the array which in loops can be detrimental to performance.
Ilija