rfc:argument_unpacking

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:argument_unpacking [2013/09/30 12:48]
nikic update implementation, mention string keys
rfc:argument_unpacking [2017/09/22 13:28] (current)
Line 2: Line 2:
   * Date: 2013-08-30   * Date: 2013-08-30
   * Author: Nikita Popov <​nikic@php.net>​   * Author: Nikita Popov <​nikic@php.net>​
-  * Status: ​Under Discussion +  * Status: ​Implemented (in PHP 5.6)
-  * Proposed for: PHP 5.6+
   * Patch: https://​github.com/​php/​php-src/​pull/​477   * Patch: https://​github.com/​php/​php-src/​pull/​477
   * Mailing list discussion: http://​markmail.org/​message/​dxae5ybjldg6pftp   * Mailing list discussion: http://​markmail.org/​message/​dxae5ybjldg6pftp
Line 40: Line 39:
 </​code>​ </​code>​
  
-It's possible to use ''​%%...%%''​ multiple times in a call and it is possible to mix it with normal arguments:+It's possible to use ''​%%...%%''​ multiple times in a call and it is possible to use normal arguments ​before argument unpacking:
  
 <code php> <code php>
Line 47: Line 46:
 test(...$args1,​ ...$args2); // [1, 2, 3, 4, 5, 6] test(...$args1,​ ...$args2); // [1, 2, 3, 4, 5, 6]
 test(1, 2, 3, ...$args2); ​  // [1, 2, 3, 4, 5, 6] test(1, 2, 3, ...$args2); ​  // [1, 2, 3, 4, 5, 6]
-test(...$args1,​ 4, 5, 6);   // [1, 2, 3, 4, 5, 6]+</​code>​ 
 + 
 +However, it is not possible to use normal arguments after argument unpacking was used. Both of the following are invalid: 
 + 
 +<code php> 
 +test(...$args1,​ 4, 5, 6); 
 +test(...$args1, 4, 5, 6, ...$args2);
 </​code>​ </​code>​
  
Line 75: Line 80:
  
 <code php> <code php>
-var_dump(1, 2, ...null, 3, 4);+var_dump(1, 2, ...null, ​...[3, 4]);
 // Warning: Only arrays and Traversables can be unpacked // Warning: Only arrays and Traversables can be unpacked
 // int(1) int(2) int(3) int(4) // int(1) int(2) int(3) int(4)
Line 94: Line 99:
 </​code>​ </​code>​
  
-By-reference passing will not work if the unpacked entity is a Traversable. Instead an ''​E_STRICT''​ level error is thrown and the argument is passed by-value instead:+By-reference passing will not work if the unpacked entity is a Traversable. Instead an ''​E_WARNING''​ level error is thrown and the argument is passed by-value instead:
  
 <code php> <code php>
 test(...new ArrayIterator([1,​ 2, 3, 4, 5])); test(...new ArrayIterator([1,​ 2, 3, 4, 5]));
-// Strict standards: Cannot pass by-reference argument 3 of test() by unpacking a Traversable,​ passing by-value instead+// Warning: Cannot pass by-reference argument 3 of test() by unpacking a Traversable,​ passing by-value instead
 </​code>​ </​code>​
  
Line 108: Line 113:
 ==== String keys ==== ==== String keys ====
  
-In order to ensure forward-compatibility with [[rfc:​named_params|named arguments]] the unpacking operator does not support string keys. If a string key is encountered during unpacking a warning ​is thrown ​and no further arguments ​are unpacked ​(but the call still happens). Alternatively only the value with the string key could be skipped. (Throwing an exception here would of course be the best solution, but is not allowed...)+In order to ensure forward-compatibility with [[rfc:​named_params|named arguments]] the unpacking operator does not support string keys. If a string key is encountered during unpacking a recoverable error is thrown. If the error is ignored using a custom error handler, ​no further arguments ​will be unpacked but the call still happens.
  
 ===== Backward Compatibility ===== ===== Backward Compatibility =====
Line 219: Line 224:
  
 The patch for this features is available as a PR: https://​github.com/​php/​php-src/​pull/​477 The patch for this features is available as a PR: https://​github.com/​php/​php-src/​pull/​477
 +
 +===== Vote =====
 +
 +As this is a language change a two third majority is required.
 +
 +Voting started 2013-12-21 and ended 2014-01-11.
 +
 +<doodle title="​Implement argument unpacking in PHP 5.6?" auth="​nikic"​ voteType="​single"​ closed="​true">​
 +   * Yes
 +   * No
 +</​doodle>​
  
 ===== Support in other languages ===== ===== Support in other languages =====
rfc/argument_unpacking.1380545281.txt.gz · Last modified: 2017/09/22 13:28 (external edit)