# clarify the destructuring list() concept

- Thread: https://externals.io/message/122126
- Original archive: https://news-web.php.net/php.internals/122126
- Started by glash.gnome@gmail.com on 2024-01-05T20:44:00+00:00
- Messages: 7
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #122126 — glash.gnome@gmail.com — 2024-01-05T20:44:00+00:00

URL: https://externals.io/message/122126#122126

Hello internals,

> The purpose of list() is to assign a list of variables.

What should be the underlying concept behind the list() language construct?

I propose that list() is the reciprocal of array().
That is, if array() is a function call, then list() is the signature of a
function.

Do you agree ?

Regards, Alexandre

PS: For historical reason, i propose to allow to syntaxe :
list(name: $name) = ['name'=>$name];// maybe discussed
list('name'=> $name) = ['name'=>$name];

---

## #122127 — Rowan Tommins — 2024-01-05T22:31:32+00:00

URL: https://externals.io/message/122126#122127 · In reply to: #122126

>Hello internals,
>
> > […]
>
>What should be the underlying concept behind the list() language construct?
>
>I propose that list() is the reciprocal of array().
>That is, if array() is a function call, then list() is the signature of a
>function.
>
>Do you agree ?
>
>Regards, Alexandre
>
>PS: For historical reason, i propose to allow to syntaxe :
>list(name: $name) = ['name'=>$name];// maybe discussed
>list('name'=> $name) = ['name'=>$name];


I'm not sure what your question is, but the second option, with => as in all PHP contexts, already works: https://3v4l.org/dqgal

Note that neither array() nor list() are functions, and both can be spelled [] as in 
['name'=> $name2] = ['name'=>$name1];
 https://3v4l.org/Uu5e4

The name for this if you want to find more information is "array destructuring".


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #122128 — glash.gnome@gmail.com — 2024-01-05T23:16:58+00:00

URL: https://externals.io/message/122126#122128

---------- Forwarded message ---------
De : Mönôme Epson <glash.gnome@gmail.com>
Date: sam. 6 janv. 2024 à 00:15
Subject: Re: [PHP-DEV] clarify the destructuring list() concept
To: Rowan Tommins <rowan.collins@gmail.com>




Le ven. 5 janv. 2024 à 23:31, Rowan Tommins <rowan.collins@gmail.com> a
écrit :

>
> I'm not sure what your question is



I try to follow the procedure of: https://wiki.php.net/rfc/howto

It seems to me that list() is not clearly specified.
I wanted to know your opinions on this.


For example :
$key = "a";
$value = 1;
$array = [$key => $value];

[$key => $value] = $array;

We all agree that now $key = 'a' and $value = 1

And I wonder why
What happens in this case :

[$key => $value] = ['b'=>2];

$key = 'b' and $value = 2 or all is null ?

Le ven. 5 janv. 2024 à 23:31, Rowan Tommins <rowan.collins@gmail.com> a
écrit :

> The name for this if you want to find more information is "array
> destructuring".


list() supports destructuring assignment for arrays. Do you have an opinion
on object destructuring ?

*Do you think seeing list() as the reciprocal of a function call is
interesting ?*

Otherwise, how to use a default value, type hinting, nullable/optional
variable... It's not interesting ?

There are many things that could be done with but don't work. For example :

$array = [1, 2, 3];
[...$values] = [...$array];

My question is: I see list() as the reciprocal of an array(). Do you think
it would be interesting for everyone if I proposed my vision?

---

## #122129 — Larry Garfield — 2024-01-05T23:47:54+00:00

URL: https://externals.io/message/122126#122129 · In reply to: #122128

> ---------- Forwarded message ---------

> I try to follow the procedure of: https://wiki.php.net/rfc/howto
>
> It seems to me that list() is not clearly specified.
> I wanted to know your opinions on this.
>
>
> For example :
> $key = "a";
> $value = 1;
> $array = [$key => $value];
>
> [$key => $value] = $array;
>
> We all agree that now $key = 'a' and $value = 1
>
> And I wonder why
> What happens in this case :
>
> [$key => $value] = ['b'=>2];
>
> $key = 'b' and $value = 2 or all is null ?

Please don't top-post.

As for what that does:

https://3v4l.org/5ubJU

That's because the right-side can have many keys with their own values, but the left side... doesn't really do that.  The left side only assigns values, not keys.

I'm not sure how it would be useful to decompose like you're describing, since it would only really make sense with a single array item on the left, which is not a common or useful case.

What you can do, as Rowan noted, is this:

https://3v4l.org/cEmcC

That pulls items out of the array by key, into named variables.

--Larry Garfield

---

## #122130 — glash.gnome@gmail.com — 2024-01-06T00:22:54+00:00

URL: https://externals.io/message/122126#122130 · In reply to: #122129

Thank you for your reply. Forgive me for a few mistakes. I'm new to
Gmail... Is this line "top-post" ?

Le sam. 6 janv. 2024 à 00:48, Larry Garfield <larry@garfieldtech.com> a
écrit :

> What you can do, as Rowan noted, is this:
>
> https://3v4l.org/cEmcC
>
> I understood your code. It is interesting when you do not know the order
of the keys.
https://3v4l.org/O29NL
I would have preferred to be able to write:
https://3v4l.org/o5jkH
In the same way I do it here:
https://3v4l.org/I4V3W

I don't see any concrete case where using a variable as a key can be useful
https://3v4l.org/WR54k

For me it was implemented like this, because it's easier.

In reality, I'm not asking "how" it actually works, but how it should work.

---

## #122132 — Rowan Tommins — 2024-01-06T11:22:22+00:00

URL: https://externals.io/message/122126#122132 · In reply to: #122128

>I try to follow the procedure of: https://wiki.php.net/rfc/howto

Then I suggest you take a moment to think about what you're proposing to change, and write it out clearly, as in "I propose to make this code ... do this ... instead of this ..." At the moment, your messages contain a lot of open-ended questions, and examples that you can easily look up the current behaviour of yourself.

Keep in mind that there are millions of existing PHP applications which might be impacted by your change. In general, changing something which is currently an error is not a problem; but if the code currently succeeds, there is a chance that somebody somewhere is using it. You need to think about how the change will affect that existing code.



>It seems to me that list() is not clearly specified.

Do you mean that the documentation of the feature is not clear? Or, that there are cases where it's behaviour is inconsistent in some way?


>list() supports destructuring assignment for arrays. Do you have an opinion
>on object destructuring ?

It might be interesting to allow an object on the right-hand side, and somehow specify properties to extract from it. I would be less interested in having an object on the left-hand side, which would presumably create an instance of stdClass,  which I find pointless. Others might disagree.



>*Do you think seeing list() as the reciprocal of a function call is
>interesting ?*

As I said previously, I would now always spell both array creation and array destructuring with [] not with array() and list(). Neither have ever behaved like function calls, and I don't think thinking of them that way is helpful.

Instead, think of it as a way of specifying the content of an array, just like "hello $name" specifies the content of a string. Then think of array destructuring as the reverse of that construction:

$arr = [$a, $b];
[$a, $b] = $arr;

$arr = ['a' => $a, 'b' => $b];
['a' => $a, 'b' => $b] = $arr;

$arr = [1 => $b, 0 => $a];
[0 => $a, 1 => $b] = $arr;

$arr = [$a, $b]; // keys 0 and 1 assigned by default
[1 => $b, 0 => $a] = $arr;

$arr = [1 => $b, 0 => $a];
[$a, $b] = $arr; // keys 0 and 1 taken by default 

$arr = ['a' => 1, $dynamicKey => 2];
[$dynamicKey => $two, 'a' => $one] = $arr;



>Otherwise, how to use a default value, type hinting, nullable/optional
>variable... It's not interesting ?

Some of those could be useful in both array creation and array destructuring, but it's not as simple as copying the syntax of a function signature. Again, I suggest you write down a specific feature you are proposing to add, and think about how it would work.



>There are many things that could be done with but don't work. For example :
>
>$array = [1, 2, 3];
>[...$values] = [...$array];

I can see this would be useful as a "rest of the values" syntax, as in:

$arr = [1,2,3,4,5];
[$first, $second, ...$rest] = $arr;

It couldn't work as the exact inverse of construction, though; this is allowed:

$arr1 = [1, 2, 3];
$arr2 = [...$arr1, ...$arr1]; 

But this wouldn't make any sense:

$arr2 = [1, 2, 3, 1, 2, 3];
[...$arr1, ...$arr1] = $arr2;

Once again, a proposal of exactly how it would work would be interesting.


Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #122134 — Larry Garfield — 2024-01-06T16:37:40+00:00

URL: https://externals.io/message/122126#122134 · In reply to: #122130

> Thank you for your reply. Forgive me for a few mistakes. I'm new to
> Gmail... Is this line "top-post" ?

Yes.  Top posting means putting your reply above what people have sent previously.  On many mailing lists, including this one, that is discouraged as it makes the thread harder to follow.  Instead, put your responses inline between parts of what you're replying to, or at the very end.  Then remove any parts of the previous messages not relevant to what you're responding to.

> Le sam. 6 janv. 2024 à 00:48, Larry Garfield <larry@garfieldtech.com> a
> écrit :
>
> > […]
> of the keys.
> https://3v4l.org/O29NL
> I would have preferred to be able to write:
> https://3v4l.org/o5jkH
> In the same way I do it here:
> https://3v4l.org/I4V3W
>
> I don't see any concrete case where using a variable as a key can be useful
> https://3v4l.org/WR54k
>
> For me it was implemented like this, because it's easier.
>
> In reality, I'm not asking "how" it actually works, but how it should work.

As Rowan notes, how it does work currently is critical, because we need to avoid breaking existing code.  Adding functionality is fine and good, but breaking people's code is not, outside of some very controlled edge cases.

> list() supports destructuring assignment for arrays. Do you have an opinion
on object destructuring ?

Object destructuring would be better done using pattern matching.  Work is already in progress on that, though it's not yet ready: https://wiki.php.net/rfc/pattern-matching

I get the sense that's much closer to what you are looking for.

--Larry Garfield
