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
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
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
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
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]
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
Am 03.11.2022 um 20:55 schrieb Tim Düsterhus tim@bastelstu.be:
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_coalesceUnless 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
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