Re: RFC proposal: Spread Operator for Array Destructuring Assignment
| From: | Nikita Popov | Date: | Wed, 03 Jun 2020 10:08:46 +0000 |
| Subject: | Re: RFC proposal: Spread Operator for Array Destructuring Assignment | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-110346@lists.php.net to get a copy of this message | ||
On Wed, Jun 3, 2020 at 12:02 PM Côme Chilliet <
come.chilliet@fusiondirectory.org> wrote:
> Le Wed, 3 Jun 2020 11:46:00 +0200,
> Nikita Popov <nikita.ppv@gmail.com> a écrit :
> > To give a tl;dr of existing discussions on the topic:
> >
> > $foobar = [2 => 2, 1 => 1, 0 => 0];
> > [$foo, ...$bar] = $foobar;
> > // What is the result?
> >
> > Introducing this feature is primarily a matter of coming up with a
> > satisfactory answer to the above question. (There is an existing
> > constraint that $foo must be equal to 0 in this case, not 2.)
>
> Well, I guess it should behave exactly the same as:
>
> $foobar = [2 => 2, 1 => 1, 0 => 0];
> [$foo, $foo2, $foo3] = $foobar;
> $bar = [$foo2, $foo3];
>
> for consistency, no?
>
To be clear, the result of your semantics would be:
$foo = 0;
$bar = [1, 2];
Which naturally leads to the question: What happens with string keys? I
guess those would just be carried over as-is? (What would the order be
though? Are integer keys reindexed before string keys?)
The other possible result would be:
$foo = 0;
$bar = [2 => 2, 1 => 1];
This leaves alone the parts of the array that were not explicitly
destructured.
Regards,
Nikita