rfc:trailing-commas-function-calls
Differences
This shows you the differences between two versions of the page.
rfc:trailing-commas-function-calls [2017/10/07 18:17] – created sammyk | rfc:trailing-commas-function-calls [2017/10/07 18:21] (current) – Delete sammyk | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Allow trailing commas in function calls ====== | + | Oops! Wrong URL - sorry! I don't know how to delete. |
- | * Version: 0.1 | + | |
- | * Date: 2017-10-06 | + | |
- | * Author: Sammy Kaye Powers, me@sammyk.me | + | |
- | * Status: Under Discussion | + | |
- | * First Published at: https:// | + | |
- | + | ||
- | ===== Introduction ===== | + | |
- | Allowing a trailing comma in function calls will make it more convenient to append arguments in many contexts where it is common to call a function with lots of arguments; especially variadic functions. | + | |
- | + | ||
- | ===== Proposal ===== | + | |
- | Trailing commas have been allowed in array syntax since forever-ever, | + | |
- | + | ||
- | <code php> | + | |
- | # Currently possible | + | |
- | use Foo\Bar\{ | + | |
- | Foo, | + | |
- | Bar, | + | |
- | }; | + | |
- | + | ||
- | $foo = [ | + | |
- | | + | |
- | ' | + | |
- | ]; | + | |
- | </ | + | |
- | + | ||
- | Allowing a trailing comma makes sense in these contexts as new values get appended frequently. There is another context wherein appending more values frequently happens: calling a variadic function. | + | |
- | + | ||
- | <code php> | + | |
- | unset( | + | |
- | $foo, | + | |
- | $bar, | + | |
- | $baz, | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ===== Frequent Context Examples ===== | + | |
- | Trailing commas in function calls make it crazy convient to invoke any function that takes variable arguments (i.e. using the splat operator < | + | |
- | + | ||
- | ==== Unsetting variables ==== | + | |
- | It is extremely common to send a list of two or more arguments to < | + | |
- | + | ||
- | <code php> | + | |
- | unset( | + | |
- | $somethingIDontNeedAnymore, | + | |
- | $anotherOneToKill, | + | |
- | $letsMakeThisEasy, | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ==== Sending variables to a template engine ==== | + | |
- | Another extremely common practice is to send a list of variables to a tempalte engine concisely using < | + | |
- | + | ||
- | <code php> | + | |
- | echo $twig-> | + | |
- | ' | + | |
- | compact( | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | ) | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ==== Merging arrays ==== | + | |
- | Invoking < | + | |
- | + | ||
- | <code php> | + | |
- | $newArray = array_merge( | + | |
- | $arrayOne, | + | |
- | $arrayTwo, | + | |
- | [' | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ==== Debugging all the things ==== | + | |
- | When you're quickly debugging with < | + | |
- | + | ||
- | <code php> | + | |
- | var_dump( | + | |
- | $whatIsInThere, | + | |
- | $probablyABugInThisOne, | + | |
- | $oneMoreToCheck, | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ==== i18n & l10n ==== | + | |
- | Internationalization & localization often makes use of variadic functions such as < | + | |
- | + | ||
- | <code php> | + | |
- | $en = ' | + | |
- | $text = sprintf( | + | |
- | $en, | + | |
- | ' | + | |
- | ' | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ==== And so on... ==== | + | |
- | This list of examples is not meant to be exhaustive, but you can see how allowing a trailing comma in function calls fits well within the existing trailing comma contexts (arrays & grouped namespaces). | + | |
- | + | ||
- | ===== Method & closure calls too ===== | + | |
- | Method calls also adopt trailing comma functionality. | + | |
- | + | ||
- | <code php> | + | |
- | class Foo | + | |
- | { | + | |
- | public function __construct(...$args) { | + | |
- | // | + | |
- | } | + | |
- | + | ||
- | public function bar(...$args) { | + | |
- | // | + | |
- | } | + | |
- | + | ||
- | public function __invoke(...$args) { | + | |
- | // | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | $foo = new Foo( | + | |
- | ' | + | |
- | ' | + | |
- | ); | + | |
- | + | ||
- | $foo-> | + | |
- | ' | + | |
- | ' | + | |
- | ); | + | |
- | + | ||
- | $foo( | + | |
- | ' | + | |
- | ' | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | And closures too. | + | |
- | + | ||
- | <code php> | + | |
- | $bar = function(...$args) { | + | |
- | // | + | |
- | }; | + | |
- | + | ||
- | $bar( | + | |
- | ' | + | |
- | ' | + | |
- | ); | + | |
- | </ | + | |
- | + | ||
- | ===== "Not really a function" | + | |
- | There are two language constructs that look like functions but aren't that will also allow trailing commas: < | + | |
- | + | ||
- | <code php> | + | |
- | unset($foo, $bar,); | + | |
- | var_dump(isset($foo, | + | |
- | </ | + | |
- | + | ||
- | ===== Wait, didn't we just vote on this? ===== | + | |
- | Yes, there was an RFC to [[https:// | + | |
- | + | ||
- | I was contacted by many " | + | |
- | + | ||
- | We are allowed to put this feature up for vote again since the mandatory 6-month waiting period has passed since the last vote and this RFC targets a new major version of PHP. | + | |
- | + | ||
- | ===== Backward Incompatible Changes ===== | + | |
- | None | + | |
- | + | ||
- | ===== Proposed PHP Version(s) ===== | + | |
- | PHP 7.3 | + | |
- | + | ||
- | ===== What's not allowed ===== | + | |
- | Function declaration syntax will not change. This RFC targets function call syntax only. | + | |
- | + | ||
- | <code php> | + | |
- | # Parse error | + | |
- | function bar($a, $b,) { | + | |
- | // | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Free-standing commas are not allowed. | + | |
- | + | ||
- | <code php> | + | |
- | # Parse error | + | |
- | foo(,); | + | |
- | </ | + | |
- | + | ||
- | Multiple trailing commas & leading commas are not allowed. | + | |
- | + | ||
- | <code php> | + | |
- | # Parse error | + | |
- | foo(' | + | |
- | # Also parse error | + | |
- | foo(, ' | + | |
- | </ | + | |
- | + | ||
- | Other things that could look like functions like < | + | |
- | + | ||
- | ===== Proposed Voting Choices ===== | + | |
- | Requires a 2/3 or 50%+1 majority | + | |
- | + | ||
- | ===== Patches and Tests ===== | + | |
- | This patch, sans the tests, is a trivial three-line change in the parser. | + | |
- | + | ||
- | [[https:// | + | |
- | + | ||
- | The trailing comma is thrown out at the parser level so there' | + | |
- | + | ||
- | ===== Implementation ===== | + | |
- | After the project is implemented, | + | |
- | - the version(s) it was merged to | + | |
- | - a link to the git commit(s) | + | |
- | - a link to the PHP manual entry for the feature | + | |
- | - a link to the language specification section (if any) | + | |
- | | + |
rfc/trailing-commas-function-calls.1507400253.txt.gz · Last modified: 2017/10/07 18:17 by sammyk