rfc:additional-splat-usage

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:additional-splat-usage [2017/09/06 02:56] – Additional info and revive this RFC! haskellcamargorfc:additional-splat-usage [2021/06/09 16:03] (current) – Status set to inactive for consistency, it was already part of the "Inactive" list patrickallaert
Line 3: Line 3:
   * Date: 2014-11-03   * Date: 2014-11-03
   * Author: Chris Wright, daverandom@php.net, Marcelo Camargo, João Lucchetta   * Author: Chris Wright, daverandom@php.net, Marcelo Camargo, João Lucchetta
-  * Status: Under Discussion+  * Status: Inactive
   * First Published at: http://wiki.php.net/rfc/additional-splat-usage   * First Published at: http://wiki.php.net/rfc/additional-splat-usage
  
Line 33: Line 33:
 The new syntax gives equivalent behaviour to the ''array_merge()'' example above, where the contents of ''$arr1'' are appended to the array literal, and stored in ''$arr2''. The same rules for merging are followed; in the case of duplicated string keys, the later value overwrites the earlier when read from left to right, and numerically indexed arrays are appropriately re-keyed and appended. Any number of variables can be unpacked at any position in the array literal, and may be combined with regular element declarations in any order. The new syntax gives equivalent behaviour to the ''array_merge()'' example above, where the contents of ''$arr1'' are appended to the array literal, and stored in ''$arr2''. The same rules for merging are followed; in the case of duplicated string keys, the later value overwrites the earlier when read from left to right, and numerically indexed arrays are appropriately re-keyed and appended. Any number of variables can be unpacked at any position in the array literal, and may be combined with regular element declarations in any order.
  
-As such, the following applications of this operator should be equivalent:+As such, the following application of ''array_merge()'' and of the splat operator and  should be equivalent:
 <code php> <code php>
-<?php 
 $array1 = ["color" => "red", "model" => "Corolla"]; $array1 = ["color" => "red", "model" => "Corolla"];
 $array2 = ["type" => "car", "year" => "2002"]; $array2 = ["type" => "car", "year" => "2002"];
Line 41: Line 40:
 $result = array_merge($array1, $array2); $result = array_merge($array1, $array2);
 $result2 = [...$array1, ...$array2]; $result2 = [...$array1, ...$array2];
-?> 
 </code> </code>
  
-Combinations of the splat operator in variable and in other literal should also work:+A mixture of applying the splat operator to previously assigned variables and other literal should work just as fine:
 <code php> <code php>
-<?php 
 $array1 = ["color" => "red", "model" => "Corolla"]; $array1 = ["color" => "red", "model" => "Corolla"];
 $array2 = ["type" => "car", "year" => "2002"]; $array2 = ["type" => "car", "year" => "2002"];
Line 52: Line 49:
 $result = array_merge($array1, ["category" => "suv", "condition" => "good"], $array2); $result = array_merge($array1, ["category" => "suv", "condition" => "good"], $array2);
 $result2 = [...$array1, ...["category" => "suv", "condition" => "good"], ...$array2]; $result2 = [...$array1, ...["category" => "suv", "condition" => "good"], ...$array2];
-?> 
 </code> </code>
 +
 +The operator should have the same behaviour for numeric and associative arrays. 
 +Multidimensional arrays should not be flattened: the splat operator should have effect in the first level keys only.
 +
 +=== Numeric ===
 +
 +<code php>
 +$threetofive = [3,4,5];
 +$zerotofive = [0, 1, 2, ...$threetofive];
 +</code>
 +
 +=== Associative ===
 +
 +<code php>
 +$addressData = ["street" => "George St.", "number" => 2];
 +$cityData = ["city" => "Brisbane", "postcode" => 4000];
 +
 +$propertyData = ["name" => "QUT", ...$addressData, ...$cityData];
 +</code>
 +
 +=== Multidimensional ===
 +
 +<code php>
 +$old_marks = [
 + "john" => [
 +    "maths" => 0,
 +    "english" => 8
 + ],
 + "jane" => [
 +    "maths" => 10,
 +    "english" => 7
 + ],
 + "joe" => [
 +    "maths" => 0,
 +    "english" => 8
 +  ]
 +];
 +
 +$new_marks = [
 +  "matthew" => [
 +    "maths" => 5,
 +    "english" => 5
 +  ],
 +  ...$old_marks
 +]; /* array(4) { ["matthew"]=> ... ["john"]=> ... ["jane"]=> ... ["joe"]=> ... }*/
 +</code>
 +
  
 Using the splat operator with elements that are not arrays and not ''Traversable'' should cause an error. Using the splat operator with elements that are not arrays and not ''Traversable'' should cause an error.
Line 107: Line 150:
   * [[https://wiki.php.net/rfc/variadics|Variadics RFC]]   * [[https://wiki.php.net/rfc/variadics|Variadics RFC]]
   * [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator]]   * [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator]]
- 
rfc/additional-splat-usage.1504666613.txt.gz · Last modified: 2017/09/22 13:28 (external edit)