[RFC] Destructuring Coalesce

php.internals

Bob Weinand

3 years ago
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

David Rodrigues

3 years ago
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

Bob Weinand

3 years ago
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

Bob Weinand

3 years ago
> 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

Rowan Collins

3 years ago
On 01/11/2022 00:05, Bob Weinand wrote:
>> 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
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]

Tim Düsterhus

3 years ago
Hi On 10/16/22 23:11, Bob Weinand wrote:
> 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

Bob Weinand

3 years ago
> Am 03.11.2022 um 20:55 schrieb Tim Düsterhus <tim@bastelstu.be>: > > Hi > > On 10/16/22 23:11, Bob Weinand wrote: >> 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
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

Côme Chilliet

3 years ago
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