rfc:spread_operator_for_array

This is an old revision of the document!


Spread Operator in Array Expression

Introduction

PHP has already supported argument unpacking (AKA spread operator) since 5.6. This RFC proposes to bring this feature to array expression.

Proposal

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,

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];

It's possible to do the expansion multiple times, and unlike argument unpacking, ... can be used anywhere. It's possible to add normal elements before or after the spread operator.

Spread operator works for both array syntax(array()) and short syntax([]).

$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
$arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111]

String keys

In order to make the behavior consistent with argument unpacking, string keys are not supported. A recoverable error will be thrown once a string key is encountered.

By-reference passing

It's not possible to unpack an array by reference.

$arr1 = [1, 2, 3];
$arr2 = [...&$arr1]; //invalid syntax

However, if elements in the array to be unpacked are stored by reference, they will be stored by reference in the new array as well.

$one = 1;
$arr1 = [&$one, 2, 3];
$arr2 = [0, ...$arr1];
var_dump($arr2);
/*
array(4) {
  [0]=>
  int(0)
  [1]=>
  &int(1)
  [2]=>
  int(2)
  [3]=>
  int(3)
}
*/

Backward Incompatible Changes

This change should not break anything.

Q & A

Advantages over array_merge

  1. 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.
  2. array_merge only supports array, while spread operator also supportes objects implementing Traversable.

... 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)

next PHP 7.x, likely 7.4

RFC Impact

To Opcache

I'm not sure if this RFC will have any impact on Opcache. I'll check it after I finish the patch.

Proposed Voting Choices

As this is a language change, a 2/3 majority is required.

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/spread_operator_for_array.1554444559.txt.gz · Last modified: 2019/04/05 06:09 by jhdxr