rfc:variadics
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:variadics [2013/08/28 14:09] – created nikic | rfc:variadics [2013/09/26 19:32] – Implemented nikic | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2013-08-27 | * Date: 2013-08-27 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
- | * Proposed for: PHP 5.6 | + | |
* Patch: https:// | * Patch: https:// | ||
- | + | * Mailing list discussion: http:// | |
- | This RFC proposes to add a dedicated syntax for variadic functions (functions taking a variable number of arguments). | + | |
===== Proposal ===== | ===== Proposal ===== | ||
Line 40: | Line 38: | ||
class MySQL implements DB { | class MySQL implements DB { | ||
public function query($query, | public function query($query, | ||
- | $stmt = $this-> | + | $stmt = $this->pdo-> |
$stmt-> | $stmt-> | ||
return $stmt; | return $stmt; | ||
Line 51: | Line 49: | ||
The '' | The '' | ||
+ | |||
+ | ==== Population of variadic parameter ==== | ||
+ | |||
+ | The following example shows how the variadic parameter '' | ||
+ | |||
+ | <code php> | ||
+ | function fn($reqParam, | ||
+ | var_dump($reqParam, | ||
+ | } | ||
+ | |||
+ | fn(1); | ||
+ | fn(1, 2); // 1, 2, [] | ||
+ | fn(1, 2, 3); // 1, 2, [3] | ||
+ | fn(1, 2, 3, 4); // 1, 2, [3, 4] | ||
+ | fn(1, 2, 3, 4, 5); // 1, 2, [3, 4, 5] | ||
+ | </ | ||
+ | |||
+ | '' | ||
==== By-reference capture ==== | ==== By-reference capture ==== | ||
Line 60: | Line 76: | ||
<code php> | <code php> | ||
class MySQL implements DB { | class MySQL implements DB { | ||
- | public function prepare($query, | + | public function prepare($query, |
- | $stmt = $this-> | + | $stmt = $this->pdo-> |
foreach ($params as $i => & | foreach ($params as $i => & | ||
$stmt-> | $stmt-> | ||
Line 83: | Line 99: | ||
<code php> | <code php> | ||
- | function array_merge(array... $arrays) { /* ... */ } | + | function array_merge(array ...$arrays) { /* ... */ } |
</ | </ | ||
- | PHP would make sure that all arguments are actually arrays. This also works for all other typehints like '' | + | PHP would make sure that all arguments are actually arrays. This also works for all other typehints like '' |
==== Prototype checks ==== | ==== Prototype checks ==== | ||
Line 118: | Line 134: | ||
public function query($query, | public function query($query, | ||
- | // INVALID: | + | // INVALID: |
public function query($query, | public function query($query, | ||
public function query($query, | public function query($query, | ||
Line 135: | Line 151: | ||
public function query($query, | public function query($query, | ||
public function query($query, | public function query($query, | ||
+ | </ | ||
+ | |||
+ | ==== Syntactic restrictions ==== | ||
+ | |||
+ | There may be only one variadic parameter and it needs to be the last parameter of the function. A variadic parameter may not have a default value. | ||
+ | |||
+ | As such all of the following are invalid: | ||
+ | |||
+ | <code php> | ||
+ | function fn(...$args, | ||
+ | function fn(...$args1, | ||
+ | function fn($arg, ...$args = []) | ||
</ | </ | ||
Line 146: | Line 174: | ||
</ | </ | ||
- | The functions will return '' | + | The functions will return '' |
==== Summary ==== | ==== Summary ==== | ||
Line 155: | Line 183: | ||
* '' | * '' | ||
* '' | * '' | ||
- | * '' | + | * '' |
The advantages of the syntax are: | The advantages of the syntax are: | ||
Line 162: | Line 190: | ||
* It is no longer necessary to '' | * It is no longer necessary to '' | ||
* It is now possible to do variadic by-reference captures | * It is now possible to do variadic by-reference captures | ||
+ | * Types can be checked with a typehint (rather than a manual loop) | ||
* Variadic prototypes can be enforce in interfaces / by inheritance | * Variadic prototypes can be enforce in interfaces / by inheritance | ||
+ | |||
+ | ===== Backwards compatibility ===== | ||
+ | |||
+ | ==== Userland ==== | ||
+ | |||
+ | This change does not break backwards compatibility for userland code. | ||
+ | |||
+ | In particular, this RFC does not propose to deprecate or remove the '' | ||
| | ||
- | ===== Impact on internals ===== | + | ==== Internal |
The '' | The '' | ||
- | For example, this is how the arginfo for '' | + | For example, this is how the arginfo for '' |
<code c> | <code c> | ||
Line 188: | Line 225: | ||
It would theoretically be possible to retain support for '' | It would theoretically be possible to retain support for '' | ||
- | Apart from this the change should be transparent from an internals point of view. Macros like '' | + | Apart from this the change should be transparent from an internals point of view. Macros like '' |
- | continue to work. | + | |
- | + | ||
- | ===== Backwards Compatability ===== | + | |
- | + | ||
- | This change does not break backwards compatability for userland code. | + | |
===== Discussion ===== | ===== Discussion ===== | ||
Line 205: | Line 237: | ||
Some possible alternative syntax and why I don't like it: | Some possible alternative syntax and why I don't like it: | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
* '' | * '' | ||
- | The proposed syntax is also used by Java. | + | The proposed syntax is also used by Java and will be used in [[http:// |
===== Patch ===== | ===== Patch ===== | ||
Patch available in PR#421: https:// | Patch available in PR#421: https:// | ||
+ | |||
+ | ===== Vote ===== | ||
+ | |||
+ | The vote started on 16.09.2013 and ended on 23.09.2013. There were 36 votes in favor and one against, as such the necessary two-third majority is met and this feature is **accepted**. | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ===== Argument unpacking ===== | ||
+ | |||
+ | The [[rfc: | ||
+ | |||
+ | <code php> | ||
+ | $db-> | ||
+ | </ |
rfc/variadics.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1