# [RFC] Destructuring Coalesce

- Thread: https://externals.io/message/118829
- Original archive: https://news-web.php.net/php.internals/118829
- Started by Bob Weinand on 2022-10-16T21:11:05+00:00
- Messages: 8
- RFC: Destructuring Coalesce (Declined) — https://externals.io/rfc/destructuring_coalesce.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #118829 — Bob Weinand — 2022-10-16T21:11:05+00:00

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

Hey,

I've written a small RFC about adding coalesce ability to list() destructuring.

This should enhance the ability to easily, concisely and readably destructure arrays with default values.

https://wiki.php.net/rfc/destructuring_coalesce <https://wiki.php.net/rfc/destructuring_coalesce>

Bob

---

## #118830 — David Rodrigues — 2022-10-16T21:56:09+00:00

URL: https://externals.io/message/118829#118830 · In reply to: #118829

I like it!

But what should happen if:

[ $a ?? '123', $b ] = [];
[ $a ?? '123', $b ] = [ 1 ];
[ $a ?? '123', $b ] = [ 1, 2 ];

It also supports ?: operator?

[ $a ?: '123' ] = []; so $a = '123'
[ $a ?: '123' ] = [ false ]; so $a = '123'
[ $a ?: '123' ] = [ 456 ]; so $a = 456


Atenciosamente,
David Rodrigues

---

## #118935 — Bob Weinand — 2022-11-01T00:05:51+00:00

URL: https://externals.io/message/118829#118935 · In reply to: #118829

> Am 16.10.2022 um 23:11 schrieb Bob Weinand <bobwei9@hotmail.com>:
> 
> Hey,
> 
> I've written a small RFC about adding coalesce ability to list() destructuring.
> 
> This should enhance the ability to easily, concisely and readably destructure arrays with default values.
> 
> https://wiki.php.net/rfc/destructuring_coalesce <https://wiki.php.net/rfc/destructuring_coalesce>
> 
> Bob

Hey, 

Given  haven't heard much feedback, I'll open the vote by end of the week,

Bob

---

## #118936 — Rowan Tommins — 2022-11-01T09:06:02+00:00

URL: https://externals.io/message/118829#118936 · In reply to: #118935

> > […]
> Hey,
>
> Given  haven't heard much feedback, I'll open the vote by end of the week,
>
> Bob


Unless I'm missing something, the one response you did get contained 
some questions that you didn't answer?


As for my own reaction: the simple examples look useful, and maybe are 
enough to include the feature. I find nested destructuring a bit 
confusing to read at the best of times, and some of the examples like 
[[$a ?? "default"] ?? []] = $array make me go a bit cross-eyed, but you 
make a reasonable case for the choice of semantics, and I don't really 
have a strong reason *not* to allow it.

Regards,

-- 
Rowan Tommins
[IMSoP]

---

## #118954 — Bob Weinand — 2022-11-03T19:38:26+00:00

URL: https://externals.io/message/118829#118954 · In reply to: #118830

Hey,

> Am 16.10.2022 um 23:56 schrieb David Rodrigues <david.proweb@gmail.com>:
> 
> I like it!
> 
> But what should happen if:
> 
> [ $a ?? '123', $b ] = [];

$a is assigned 123, $b emits undefined key error.

> [ $a ?? '123', $b ] = [ 1 ];

$a is assigned 1, $b emits undefined key error.

> [ $a ?? '123', $b ] = [ 1, 2 ];

$a is assigned 1, $b is assigned 2.

> It also supports ?: operator?
> 
> [ $a ?: '123' ] = []; so $a = '123'
> [ $a ?: '123' ] = [ false ]; so $a = '123'
> [ $a ?: '123' ] = [ 456 ]; so $a = 456

?: is out of scope of this RFC; a future RFC may want to take care of that.

Bob

---

## #118955 — tim@bastelstu.be — 2022-11-03T19:55:05+00:00

URL: https://externals.io/message/118829#118955 · In reply to: #118829

Hi


> I've written a small RFC about adding coalesce ability to list() destructuring.
> 
> This should enhance the ability to easily, concisely and readably destructure arrays with default values.
> 
> https://wiki.php.net/rfc/destructuring_coalesce <https://wiki.php.net/rfc/destructuring_coalesce>

Unless I missed anything, all the examples in the RFC deal with *absent* 
array keys. Can you also add an example where the key is present, but 
the value is 'null' to make the behavior with regard to that explicit?

Best regards
Tim Düsterhus

---

## #118956 — Bob Weinand — 2022-11-03T20:10:02+00:00

URL: https://externals.io/message/118829#118956 · In reply to: #118955

> Am 03.11.2022 um 20:55 schrieb Tim Düsterhus <tim@bastelstu.be>:
> 
> Hi
> 
> 
> > […]
> 
> Unless I missed anything, all the examples in the RFC deal with *absent* array keys. Can you also add an example where the key is present, but the value is 'null' to make the behavior with regard to that explicit?
> 
> Best regards
> Tim Düsterhus

There was actually one line saying ... if  $array[0] is null or does not exist, but I've added a more specific example under use cases.

Bob

---

## #118987 — come@chilliet.eu — 2022-11-08T17:09:29+00:00

URL: https://externals.io/message/118829#118987 · In reply to: #118829

Le dimanche 16 octobre 2022, 23:11:05 CET Bob Weinand a écrit :
> Hey,
> 
> I've written a small RFC about adding coalesce ability to list() destructuring.
> 
> This should enhance the ability to easily, concisely and readably destructure arrays with default values.
> 
> https://wiki.php.net/rfc/destructuring_coalesce <https://wiki.php.net/rfc/destructuring_coalesce>
> 
> Bob

Hello,

I voted no as the syntax is too confusing. It reads as if coalescing would happen on the var while it happens on the array value.
It means $a ?? 'value' would mean something different when in a list context.

It also looks wrong with keys:
list("name" => $name ?? "unknown") = json_decode($json) ?: [];
I would expect:
list("name" ?? "unknown" => $name) = json_decode($json) ?: [];

But event then it looks fishy since "name" is a key and "unknown" is a value.

I understand the need but I just can’t see a good syntax for it, and the one chosen in the RFC sure feels not good enough for integration.

Côme
