Hi Ilija,
* Are integer keys preserved? I'm assuming no, as otherwise it would
be the same as `$a + $b`.
$arr[...] = $arr;
should be the same as
foreach ($arr as $v) { $arr[] = $v; }
all other answers should be answered from this and consistency
* What is the return value of the expression `$a[...] = $b`? I'm
assuming $a after the additions of $b?
https://3v4l.org/63IEU - the right side expr to be consistent with regular
$arr[] = expr
* How does it behave in combination with `ArrayAccess`? Throw? Call
`offsetSet` for each element?
https://3v4l.org/v26Bh - 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)
`assign_by_ref($a[...])` would be about the same as `assign_by_ref($a)`, so
compile error
* How does it interact with undefined/null values? E.g. `$a[] = 42;`
works without declaring $a first.
https://3v4l.org/JtO7g - same behaviour, based on the foreach definition
* 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.
Yes, `+` and/or `array_push($a, ...$b)` are not equivalents, and all other
syntaxes like
`[...$a, ...$b]` or `array_merge($a, $b)` are longer to write and slower
* One extra QA about references:
https://3v4l.org/U02Cq - `$arr[...] = &$arr` should be compile error
Michael
________________________________
From: Ilija Tovilo <[email protected]>
Sent: Thursday, April 6, 2023 12:18 PM
To: [email protected] <[email protected]>
Subject: Re: [PHP-DEV] Array spread append
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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php