rfc:trailing-comma-function-args
no way to compare when less than two revisions

Differences

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


Previous revision
Next revision
rfc:trailing-comma-function-args [2013/02/21 03:02] tyrael
Line 1: Line 1:
 +====== Request for Comments: Trailing comma function args ======
 +  * Version: 1.0
 +  * Date: 2013-02-19
 +  * Author: Sara Golemon <pollita@php.net>
 +  * Status: Under Discussion
 +  * First Published at: http://wiki.php.net/rfc/trailing-comma-function-args
  
 +===== Introduction =====
 +
 +PHP has long supported trailing commas in array declarations like so:
 +
 +  <?php
 +  $a = array(
 +         'Orange',
 +         'Apple',
 +         'Banana',
 +        );
 +
 +This is useful for VCS (cvs, svn, git, hg, etc...) since individual lines in the list may be modified, added, or removed without having to touch unrelated lines which happen to be at the end of the list.
 +
 +Function call arguments do not share this trait.
 +
 +  <?php
 +  // This is an error
 +  $fp = fopen(
 +          "sample.txt",
 +          "r+",
 +         );
 +
 +Which means that adding additional parameters to the call forces touching both the new line and the prior one, which is bad for VCS history.
 +
 +Similarly, function declarations have the same inconsistency with array() and list().
 +
 +  <?php
 +  // Invalid currently
 +  function foo(
 +             $bar,
 +             ) {
 +    /* ... */
 +  }
 +
 +This RFC proposes to allow trailing commas in function and method call argument lists and function argument declarations.
 +
 +===== The patch =====
 +
 +Could not be much simpler...
 +
 +  diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
 +  index ccbc9b1..6b6de2e 100644
 +  --- a/Zend/zend_language_parser.y
 +  +++ b/Zend/zend_language_parser.y
 +  @@ -515,7 +515,7 @@ new_else_single:  
 +   
 +   
 +   parameter_list:
 +  -               non_empty_parameter_list
 +  +               non_empty_parameter_list possible_comma
 +          |       /* empty */
 +   ;
 +   
 +  @@ -542,7 +542,7 @@ optional_class_type:
 +  
 +   function_call_parameter_list:
 +                  '(' ')' { Z_LVAL($$.u.constant) = 0; }
 +  -             '(' non_empty_function_call_parameter_list ')'  { $$ = $2; }
 +  +             '(' non_empty_function_call_parameter_list possible_comma ')'   { $$ = $2; }
 +          |       '(' yield_expr ')'      { Z_LVAL($$.u.constant) = 1; zend_do_pass_param(&$2, ZEND_SEND_VAL, Z_LVAL($$.u.constant) TSRMLS_CC); }
 +   ;
 +   
 +
 +===== What's left out on purpose =====
 +
 +Bonus comma in the middle of a void argument list
 +  * It implies two ignored args, rather than one
 +  * It doesn't actually help the VCS blame issue anyway
 +
 +  <?php
 +  phpinfo(
 +      ,
 +      );
 +
 +Trailing comma in yield() expression
 +  * yield only takes one value (or key=>val expression)
 +  * yield is not really a function call, even if it looks like one
 +
 +  <?php
 +  function mygen() {
 +    yield(
 +      123,
 +      );
 +  }
 +
 +===== Tests =====
 +
 +Yes, I'll write some.
 +
 +===== Changelog =====
 +
 +  * Expanded proposal to include declarations as well as arguments 2013-02-20 11:23 GMT
rfc/trailing-comma-function-args.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1