rfc:destructuring_coalesce
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:destructuring_coalesce [2022/10/14 15:52] – use cases bwoebi | rfc:destructuring_coalesce [2022/11/26 15:07] (current) – updated status. danack | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2022-10-14 | * Date: 2022-10-14 | ||
* Author: Bob Weinand < | * Author: Bob Weinand < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 12: | Line 12: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes the usage of the ''??'' | + | This RFC proposes the usage of the ''??'' |
In its simplest form the destructuring coalesce will be written as follows: | In its simplest form the destructuring coalesce will be written as follows: | ||
Line 27: | Line 27: | ||
[[$a, $b] ?? [1, 2]] = $array; // if $array[0] is null or does not exist, $a will be 1 and $b will be 2 | [[$a, $b] ?? [1, 2]] = $array; // if $array[0] is null or does not exist, $a will be 1 and $b will be 2 | ||
</ | </ | ||
+ | |||
+ | Also note that, equivalently to how ''??'' | ||
==== Use cases ==== | ==== Use cases ==== | ||
+ | |||
Exploding an externally provided string, e.g. a key-value pair separated by '' | Exploding an externally provided string, e.g. a key-value pair separated by '' | ||
<PHP> | <PHP> | ||
Line 49: | Line 52: | ||
) = json_decode($json) ?: []; | ) = json_decode($json) ?: []; | ||
// $name = "Bob Weinand", | // $name = "Bob Weinand", | ||
+ | </ | ||
+ | |||
+ | Filling in '' | ||
+ | <PHP> | ||
+ | $data = [1, 2, null]; | ||
+ | list($a, $b, $c ?? 3) = $data; | ||
+ | // $a = 1, $b = 2, $c = 3 | ||
</ | </ | ||
==== Semantics ==== | ==== Semantics ==== | ||
- | Note that for nested destructuring, | + | The semantics |
+ | |||
+ | An expression like | ||
<PHP> | <PHP> | ||
- | [[$a ?? " | + | [[$a]] = $array; |
</ | </ | ||
+ | is currently equivalent to (with '' | ||
+ | <PHP> | ||
+ | $tmp = $array; | ||
+ | $tmp = $tmp[0]; | ||
+ | $a = $tmp[0]; | ||
+ | </ | ||
+ | |||
+ | Thus an expression like | ||
+ | <PHP> | ||
+ | $array = []; | ||
+ | [[$a ?? " | ||
+ | </ | ||
+ | will be equivalent to: | ||
+ | <PHP> | ||
+ | $tmp = $array; | ||
+ | $tmp = $tmp[0]; // no coalesce here, emits an undefined key warning | ||
+ | $a = $tmp[0] ?? " | ||
+ | </ | ||
+ | |||
+ | Meaning that the existence of the container is always checked (producing undefined variable or key warnings if absent), only the the immediate entry which the coalesce operator is applied to never produces warnings if absent. | ||
It however is trivial to skip this check too, by defaulting the first nesting level to an empty array: | It however is trivial to skip this check too, by defaulting the first nesting level to an empty array: | ||
<PHP> | <PHP> | ||
- | [[$a ?? " | + | [[$a ?? " |
+ | // equivalent to: | ||
+ | $tmp = $array; | ||
+ | $tmp = $tmp[0] ?? []; | ||
+ | $a = $tmp[0] ?? " | ||
</ | </ | ||
Line 66: | Line 102: | ||
<PHP> | <PHP> | ||
[$a ?? " | [$a ?? " | ||
- | // Fix it via [$a ?? " | + | // equivalent to: |
+ | $tmp = $undefinedVariable; | ||
+ | $a = $tmp[0] ?? " | ||
+ | </ | ||
+ | |||
+ | Which can be coalesced away with: | ||
+ | < | ||
+ | [$a ?? " | ||
+ | // equivalent to | ||
+ | $tmp = $undefinedVariable ?? []; | ||
+ | $a = $tmp[0] ?? " | ||
</ | </ | ||
Line 85: | Line 131: | ||
With this proposal that issue does not exist and it is also not expected to be a major loss in ergonomics, as oftentimes, when you destructure, | With this proposal that issue does not exist and it is also not expected to be a major loss in ergonomics, as oftentimes, when you destructure, | ||
- | It actually even allows one to concisely express nested array access with key checking, i.e., supposing we currently write code like '' | + | It actually even allows one to concisely express nested array access with key checking, i.e., supposing we currently write code like |
+ | < | ||
+ | $value = $array[0][" | ||
+ | </ | ||
+ | and we expect everything to exist, except possibly the last '' | ||
+ | < | ||
+ | [[" | ||
+ | </ | ||
+ | checking for the existence of '' | ||
+ | |||
+ | ==== Discussion of syntax ==== | ||
+ | |||
+ | This RFC proposes using ''??'' | ||
+ | |||
+ | Other languages like Javascript have constructs like '' | ||
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 95: | Line 155: | ||
There' | There' | ||
- | ===== Proposed Voting Choices | + | ===== Vote ===== |
- | Add a destructuring coalesce feature as described? | + | A 2/3 majority is required. The vote started 2022-11-07 and ended 2022-11-21. |
- | A 2/3 majority is required. | + | <doodle title=" |
+ | * Yes | ||
+ | * No | ||
+ | </doodle> | ||
===== Implementation ===== | ===== Implementation ===== |
rfc/destructuring_coalesce.1665762753.txt.gz · Last modified: 2022/10/14 15:52 by bwoebi