rfc:destructuring_coalesce

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:destructuring_coalesce [2022/10/14 17:09] bwoebirfc: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 <bwoebi@php.net>   * Author: Bob Weinand <bwoebi@php.net>
-  * Status: Draft+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/destructuring_coalesce   * First Published at: http://wiki.php.net/rfc/destructuring_coalesce
  
Line 12: Line 12:
 ===== Proposal ===== ===== Proposal =====
  
-This RFC proposes the usage of the ''??'' coalescing operator in destructuring assignments, allowing for default values to be specified when an array key specified on the left-hand side does not exist in the destructured array.+This RFC proposes the usage of the ''??'' coalescing operator in destructuring assignments, allowing for default values to be specified when an array key specified on the left-hand side does not exist or is null in the destructured array.
  
 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
 </PHP> </PHP>
 +
 +Also note that, equivalently to how ''??'' operates, the right-hand side is not evaluated at all, if the respective key exists and is not null in the source array.
  
 ==== 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", $zip = "not provided", $locality = "Luxembourg" // $name = "Bob Weinand", $zip = "not provided", $locality = "Luxembourg"
 +</PHP>
 +
 +Filling in ''null'' values on an array of data:
 +<PHP>
 +$data = [1, 2, null];
 +list($a, $b, $c ?? 3) = $data;
 +// $a = 1, $b = 2, $c = 3
 </PHP> </PHP>
  
Line 130: Line 140:
 </PHP> </PHP>
 checking for the existence of ''$array'', ''$array[0]'' and ''$array[0]["nested"]'' (and emitting warnings upon absence). The RFC author does not recommend writing code this way in general, but acknowledges that this is a possibility enabled by the outlined semantics. checking for the existence of ''$array'', ''$array[0]'' and ''$array[0]["nested"]'' (and emitting warnings upon absence). The RFC author does not recommend writing code this way in general, but acknowledges that this is a possibility enabled by the outlined semantics.
 +
 +==== Discussion of syntax ====
 +
 +This RFC proposes using ''??'' as the default-operator for the simple fact that it actually does a coalescing operation. It is directly interpreted as a coalescing operation, with the same semantics: null and non existent keys are handled the same.
 +
 +Other languages like Javascript have constructs like ''({ key: value = default } = object);'', with an equals sign before the default value. In particular, this matches the semantics Javascript also has for function parameter defaults: undefined and non-existent values are handled the same. So this makes sense in Javascript, but not in PHP.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 139: Line 155:
 There's no BC break, not even a parser change, only not emitting a compiler error for coalesce in destructuring assignments. There's no BC break, not even a parser change, only not emitting a compiler error for coalesce in destructuring assignments.
  
-===== 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="Add a destructuring coalesce feature as described?" auth="bwoebi" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Implementation ===== ===== Implementation =====
rfc/destructuring_coalesce.1665767350.txt.gz · Last modified: 2022/10/14 17:09 by bwoebi