rfc:argument_unpacking

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
Last revisionBoth sides next revision
rfc:argument_unpacking [2013/12/21 12:49] – Start voting nikicrfc:argument_unpacking [2014/07/31 20:32] – fix now invalid code nikic
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: In Voting +  * 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 214: Line 219:
     }     }
 } }
-</code> 
- 
-==== Normal arguments after unpacked arguments ==== 
- 
-Some people also had doubts regarding the usefulness of allowing ''%%foo(...$args, $arg)%%'' calls, where a normal argument follows after an unpacked argument. 
- 
-One use-case for this pattern are the variants of ''array_diff'' and ''array_intersect'' which accept callback functions as the **last** one or two arguments: 
- 
-<code php> 
-array_udiff(...$arrays, $valueCmpFunction); 
-array_udiff_uassoc(...$arrays, $valueCmpFunction, $keyCmpFunction); 
-</code> 
- 
-While this kind of API was undoubtedly a bad choice in the first place, I see little reason not to support calls to them. 
- 
-Another use case is appending one additional argument to a variadic argument list. For example, the following snippet adds an additional ''LIMIT'' parameter to an existing query: 
- 
-<code php> 
-$this->query($query . ' LIMIT ?', ...$params, $limit); 
 </code> </code>
  
Line 243: Line 229:
 As this is a language change a two third majority is required. As this is a language change a two third majority is required.
  
-Voting started 2013-12-21 and will be open for at least two weeks (holidays :)+Voting started 2013-12-21 and ended 2014-01-11.
  
-<doodle title="Implement argument unpacking in PHP 5.6?" auth="nikic" voteType="single" closed="false">+<doodle title="Implement argument unpacking in PHP 5.6?" auth="nikic" voteType="single" closed="true">
    * Yes    * Yes
    * No    * No
rfc/argument_unpacking.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1