rfc:spread_operator_for_array

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
Next revisionBoth sides next revision
rfc:spread_operator_for_array [2018/11/19 08:16] jhdxrrfc:spread_operator_for_array [2019/04/05 06:09] jhdxr
Line 1: Line 1:
-====== Spread Operator in Array ====== +====== Spread Operator in Array Expression ====== 
-  * Version: 0.1+  * Version: 0.2
   * Date: 2018-10-13   * Date: 2018-10-13
   * Author: CHU Zhaowei, jhdxr@php.net   * Author: CHU Zhaowei, jhdxr@php.net
-  * Status: Draft+  * Status: Under discussion
   * First Published at: http://wiki.php.net/rfc/spread_operator_for_array   * First Published at: http://wiki.php.net/rfc/spread_operator_for_array
  
 ===== Introduction ===== ===== Introduction =====
-PHP has already supported [[rfc:argument_unpacking|argument unpacking]] (AKA spread operator) since 5.6. This RFC proposes to bring this feature to array.+PHP has already supported [[rfc:argument_unpacking|argument unpacking]] (AKA spread operator) since 5.6. This RFC proposes to bring this feature to array expression.
  
 ===== Proposal ===== ===== Proposal =====
-An array pair prefixed by ... will be expanded in places during array definition. Only arrays and objects who implement Traversable can be expanded.+An array pair prefixed by ''...'' will be expanded in places during array definition. Only arrays and objects who implement Traversable can be expanded.
  
 For example, For example,
Line 31: Line 31:
 ==== String keys ==== ==== String keys ====
  
-It's possible to unpack arrays with string keys. When unpacking multiple arrays with same string key, the later value for that key will overwrite the previous one. +In order to make the behavior consistent with [[rfc:argument_unpacking|argument unpacking]], string keys are not supportedA recoverable error will be thrown once a string key is encountered.
- +
-For arrays with numeric keys, they will be renumbered with incrementing keys starting from zero. So if unpack multiple arrays with same numeric key, the later value will be appended instead of overwriting the previous one. +
- +
-It's possible to unpack arrays with mixed string and numeric keys. +
-<code php> +
-$arr1 = ['a' => 1, 'b' => 2, 'c'=> 3]; +
-$arr2 = [11 => 11, 22 => 22, 33 => 33]+
-$arr3 = [111 => 9, 22 => 8, 'c' => 7]+
-var_dump([...$arr1...$arr2]); +
-/* +
-array(6) { +
-  ["a"]=> +
-  int(1) +
-  ["b"]=> +
-  int(2) +
-  ["c"]=> +
-  int(3) +
-  [0]=> +
-  int(11) +
-  [1]=> +
-  int(22) +
-  [2]=> +
-  int(33) +
-+
-*/ +
-var_dump([...$arr1, ...$arr3]); +
-/* +
-array(5) {  +
-  ["a"]=>   +
-  int(1)    +
-  ["b"]=>   +
-  int(2)    +
-  ["c"]=>   +
-  int(7)    +
-  [0]=>     +
-  int(9)    +
-  [1]=>     +
-  int(8)    +
-}           +
-*/ +
-var_dump([...$arr2, ...$arr3]); +
-/* +
-array(6) { +
-  [0]=> +
-  int(11) +
-  [1]=> +
-  int(22) +
-  [2]=> +
-  int(33) +
-  [3]=> +
-  int(9) +
-  [4]=> +
-  int(8) +
-  ["c"]=> +
-  int(7) +
-+
-*/ +
-</code> +
- +
-Actually there are several ways to handle string keys in PHP now: +
-1keep string keys +
-2. discard string keys, append the values only +
-3. raise warning / error for string keys +
- +
-This RFC adopts option 1 for the following reasons. Since the spread operator here is an extension for array defination,and the array defination syntax allows array with string keys, option 3 has been ruled out. Prior to this RFC, ''array_merge()'' does the similar job, and it's reasonable to follow the same behaviour in order to not confusing userland developers.+
  
 ==== By-reference passing ==== ==== By-reference passing ====
Line 102: Line 37:
 <code php> <code php>
 $arr1 = [1, 2, 3]; $arr1 = [1, 2, 3];
-$arr2 = [...&$arr1]; //invalid+$arr2 = [...&$arr1]; //invalid syntax
 </code> </code>
  
Line 125: Line 60:
 </code> </code>
  
 +===== Backward Incompatible Changes =====
 +This change should not break anything.
  
 +===== Q & A =====
 +==== Advantages over array_merge ====
 +  - Spread operator should have a better performance than ''array_merge''. It's becuase not only that spread operator is a language structure while ''array_merge'' is a function call, but also compile time optimiztion can be performaned for constant arrays.
 +  - ''array_merge'' only supports array, while spread operator also supportes objects implementing ''Traversable''
  
-===== Backward Incompatible Changes ===== +==== ... should be preserved for other use (e.g. map concat) ==== 
-This change should not break anything.+This is kind of out of scope here to discuss other concat / merge operation. The important thing is we should make the behavior of same operator consistent and not to confuse userland developer. It's also why I changed the behavior for string keys in this revised version.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
rfc/spread_operator_for_array.txt · Last modified: 2019/05/13 12:45 by nikic