rfc:destructuring_coalesce
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:destructuring_coalesce [2022/10/14 15:32] – bwoebi | rfc:destructuring_coalesce [2022/11/26 01:10] – bwoebi | ||
---|---|---|---|
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 28: | Line 28: | ||
</ | </ | ||
- | Note that for nested destructuring, the coalescing does **not** skip the existence check of its container. Thus the following will emit an undefined | + | Also note that, equivalently to how ''??'' |
+ | |||
+ | ==== Use cases ==== | ||
+ | |||
+ | Exploding | ||
<PHP> | <PHP> | ||
- | [[$a ?? "default"]] = []; | + | $input = " |
+ | [$key, $val ?? null] = explode(' | ||
+ | // $key = "key", $val = " | ||
+ | |||
+ | $input = " | ||
+ | [$key, $val ?? null] = explode(' | ||
+ | // $key = " | ||
</ | </ | ||
+ | |||
+ | Safely having default values on an externally provided json: | ||
+ | <PHP> | ||
+ | $json = ' | ||
+ | list( | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ) = json_decode($json) ?: []; | ||
+ | // $name = "Bob Weinand", | ||
+ | </ | ||
+ | |||
+ | Filling in '' | ||
+ | <PHP> | ||
+ | $data = [1, 2, null]; | ||
+ | list($a, $b, $c ?? 3) = $data; | ||
+ | // $a = 1, $b = 2, $c = 3 | ||
+ | </ | ||
+ | |||
+ | ==== Semantics ==== | ||
+ | |||
+ | The semantics of list destructuring are generally preserved. | ||
+ | |||
+ | An expression like | ||
+ | <PHP> | ||
+ | [[$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 41: | 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 60: | 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 '' | ||
- | ===== Proposed PHP Version(s) ===== | + | ==== Discussion of syntax |
- | Next minor version. | + | This RFC proposes using ''??'' |
- | ===== Proposed | + | Other languages like Javascript have constructs like '' |
+ | |||
+ | ===== Proposed | ||
- | Add a destructuring coalesce feature as described? | + | PHP 8.3 |
- | A 2/3 majority is required. | + | ===== Compatibility ===== |
- | ===== Patches and Tests ===== | + | There' |
- | Links to any external patches and tests go here. | + | |
- | If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed. | + | ===== Vote ===== |
- | Make it clear if the patch is intended to be the final patch, or is just a prototype. | + | A 2/3 majority |
- | For changes affecting the core language, you should also provide | + | <doodle title=" |
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Implementation ===== | ===== Implementation ===== |
rfc/destructuring_coalesce.txt · Last modified: 2022/11/26 15:07 by danack