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
rfc:spread_operator_for_array [2018/10/28 03:29] jhdxrrfc:spread_operator_for_array [2019/05/13 12:45] (current) – Implemented nikic
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: Implemented (in PHP 7.4)
   * 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 22: Line 22:
  
 Spread operator works for both array syntax(''array()'') and short syntax(''[]''). Spread operator works for both array syntax(''array()'') and short syntax(''[]'').
 +
 +It's also possible to unpack array returned by a function immediately.
 +
 <code php> <code php>
 $arr1 = [1, 2, 3]; $arr1 = [1, 2, 3];
Line 27: Line 30:
 $arr3 = [0, ...$arr1]; //[0, 1, 2, 3] $arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
 $arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111] $arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111]
-</code>+$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]
  
-It's possible to unpack arrays with string keys. When unpacking multiple arrays with same string keythe later value for that key will overwrite the previous one.+function getArr() { 
 +  return ['a', 'b']; 
 +
 +$arr6 = [...getArr(), 'c']; //['a', 'b', 'c']
  
-For arrays with numeric keys, they will be renumbered with incrementing keys starting from zeroSo if unpack multiple arrays with same numeric key, the later value will be appended instead of overwriting the previous one.+$arr7 = [...new ArrayIterator(['a', 'b', 'c'])]; //['a', 'b', 'c']
  
-It's possible to unpack arrays with mixed string and numeric keys. +function arrGen() { 
-<code php> + for($= 11; $i < 15; $i++) { 
-$arr1 = ['a' => 1, 'b' => 2, 'c'=> 3]; + yield $i; 
-$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)+
 } }
-*/ +$arr8 = [...arrGen()]//[11, 12, 13, 14]
-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> </code>
 +
 +
 +==== String keys ====
 +
 +In order to make the behavior consistent with [[rfc:argument_unpacking|argument unpacking]], string keys are not supported. A recoverable error will be thrown once a string key is encountered.
  
 ==== By-reference passing ==== ==== By-reference passing ====
Line 93: Line 56:
 <code php> <code php>
 $arr1 = [1, 2, 3]; $arr1 = [1, 2, 3];
-$arr2 = [...&$arr1]; //invalid+$arr2 = [...&$arr1]; //invalid syntax
 </code> </code>
  
Line 115: Line 78:
 */ */
 </code> </code>
- 
- 
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
 This change should not break anything. This change should not break anything.
 +
 +===== Q & A =====
 +==== Advantages over array_merge ====
 +  - Spread operator should have a better performance than ''array_merge''. It's because not only that spread operator is a language structure while ''array_merge'' is a function call, but also compile time optimization can be performant for constant arrays.
 +  - ''array_merge'' only supports array, while spread operator also supports objects implementing ''Traversable''.
 +<PHP>
 +// Before
 +array_merge(iterator_to_array($iter1), iterator_to_array($iter2))
 +
 +// Or to generalize to all iterables
 +array_merge(
 +  is_array($iter1) ? $iter1 : iterator_to_array($iter1),
 +  is_array($iter2) ? $iter2 : iterator_to_array($iter2)
 +)
 +
 +// After (handles both cases)
 +[ ...$iter1, ...$iter2 ]
 +
 +//Thanks Rowan for providing this example
 +</PHP>
 +
 +==== ... should be preserved for other use (e.g. map concat) ====
 +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) =====
Line 126: Line 110:
 ===== RFC Impact ===== ===== RFC Impact =====
 ==== To Opcache ==== ==== To Opcache ====
-I'm not sure if this RFC will have any impact on Opcache. I'll check it after I finish the patch.+Some changes in opcache to support the new opcode is needed. 
  
 +===== Vote =====
 +Voting started 2019-04-22 and ends 2019-05-06. A 2/3 majority is required.
  
-===== Proposed Voting Choices ===== +<doodle title="Support spread operator for array definition in PHP 7.4?" auth="jhdxr" voteType="single" closed="true"> 
-As this is a language change, a 2/3 majority is required. +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/spread_operator_for_array.1540697353.txt.gz · Last modified: 2018/10/28 03:29 by jhdxr