rfc:list-syntax-trailing-commas

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:list-syntax-trailing-commas [2015/11/03 19:01]
sammyk created
rfc:list-syntax-trailing-commas [2017/09/22 13:28] (current)
Line 1: Line 1:
 ====== PHP RFC: Trailing Commas In List Syntax ====== ====== PHP RFC: Trailing Commas In List Syntax ======
   * Version: 0.1   * Version: 0.1
-  * Date: 2015-11-03+  * Date: 2015-11-03 ​(discussion);​ 2017-01-27 (voting)
   * Author: Sammy Kaye Powers, me@sammyk.me   * Author: Sammy Kaye Powers, me@sammyk.me
-  * Status: ​Draft+  * Status: ​Implemented in PHP 7.2 (Grouped namepaces only)
   * First Published at: https://​wiki.php.net/​rfc/​revisit-trailing-comma-function-args,​ https://​wiki.php.net/​rfc/​trailing-comma-function-args   * First Published at: https://​wiki.php.net/​rfc/​revisit-trailing-comma-function-args,​ https://​wiki.php.net/​rfc/​trailing-comma-function-args
  
 ===== Introduction ===== ===== Introduction =====
 +
 +This RFC proposes allowing trailing commas for all list syntax.
  
 Per the feedback on the internals list, this RFC broadens the scope of the [[rfc:​revisit-trailing-comma-function-args|original RFC to allow trailing commas in function arguments]] to all list syntax. Per the feedback on the internals list, this RFC broadens the scope of the [[rfc:​revisit-trailing-comma-function-args|original RFC to allow trailing commas in function arguments]] to all list syntax.
Line 22: Line 24:
 This makes for clean diffs and easy appending of new values in user-land. This makes for clean diffs and easy appending of new values in user-land.
  
-Unfortunately,​ the other list syntaxes in PHP do not share the same luxury.+Unfortunately,​ the other lists do not share the same luxury.
  
 === Raises a parse error === === Raises a parse error ===
 <code php> <code php>
-// Parse error +<?php 
-myFunc( +use Foo\Bar\{ 
-    ​$foo+    ​Foo
-    ​$bar+    ​Bar
-    ​);+    ​Baz, 
 +};
 </​code>​ </​code>​
  
Line 38: Line 41:
  
   - Offer a consistent API and match the existing array functionality.   - Offer a consistent API and match the existing array functionality.
-  - Make it easy to append new elements in user-land. +  - Make it easy to append new elements ​to a list in user-land. 
-  - Have cleaner diffs (so only one line shows up as being modified when appending ​arguments ​instead of two). +  - Have cleaner diffs (so only one line shows up as being modified when appending ​elements ​instead of two). 
-  - It makes code reviews a tiny bit faster and for really large codebases, that time adds up. ([[https://​people.php.net/​user.php?​username=pollita|Sara Golemon]] explained [[https://​www.mail-archive.com/​internals@lists.php.net/​msg81138.html|why HHVM added the trailing comma syntax]]. See the [[https://​github.com/​facebook/​hiphop-php/​commit/​c1b7da6a4c128af5ddfc75f515c205de9f417a1e|HHVM patch]].)+  - It makes code reviews a tiny bit faster and for really large codebases, that time adds up. ([[https://​people.php.net/​user.php?​username=pollita|Sara Golemon]] explained [[https://​www.mail-archive.com/​internals@lists.php.net/​msg81138.html|why HHVM added the trailing comma syntax]].)
  
-The list syntaxes that would be affected are:+The following lists would allow trailing commas:
  
   - Grouped namepaces   - Grouped namepaces
Line 49: Line 52:
   - Trait implementations on a class   - Trait implementations on a class
   - Class member lists   - Class member lists
 +  - Inheriting variables from the parent scope in anonymous functions
  
 [[https://​people.php.net/​user.php?​username=marcio|Marcio Almada]] posted a gist with [[https://​gist.github.com/​marcioAlmada/​75f8f1d47da5dcac2e57|examples of trailing commas]] for the various lists (shown below): [[https://​people.php.net/​user.php?​username=marcio|Marcio Almada]] posted a gist with [[https://​gist.github.com/​marcioAlmada/​75f8f1d47da5dcac2e57|examples of trailing commas]] for the various lists (shown below):
Line 57: Line 61:
 // Grouped namepaces // Grouped namepaces
 use Foo\Bar\{ Foo, Bar, Baz, }; use Foo\Bar\{ Foo, Bar, Baz, };
 +
 // Arrays (already possible) // Arrays (already possible)
 $array = [1, 2, 3,]; $array = [1, 2, 3,];
 +
 // Function/​method arguments (call) // Function/​method arguments (call)
 fooCall($arg1,​ $arg2, $arg3,); fooCall($arg1,​ $arg2, $arg3,);
 +
 class Foo implements class Foo implements
     // Interface implementations on a class     // Interface implementations on a class
Line 86: Line 93:
         $c = '​baz',​         $c = '​baz',​
     ;     ;
-    ​ 
     private     private
         $blah,         $blah,
Line 96: Line 102:
     }     }
 } }
 +
 +// Inheriting variables from the parent scope in anonymous functions
 +$foo = function ($bar) use (
 +    $a,
 +    $b,
 +    $c,
 +) {
 + // . . . 
 +};
 </​code>​ </​code>​
  
Line 116: Line 131:
 ==== Why allocate resources to make this happen ==== ==== Why allocate resources to make this happen ====
  
-The actual implementation for adding tailing commas to function arguments/​calls is two lines. Implementing the functionality to all lists would not require many more changes to the php-src codebase.+The actual implementation for adding tailing commas to function arguments/​calls is [[https://​github.com/​sgolemon/​php-src/​compare/​master...trailing-comma#​diff-7eff82c2c5b45db512a9dc49fb990bb8L517|two lines]]. Implementing the functionality to all lists would not require many more changes to the php-src codebase.
  
-==== You could use leading commas ====+==== You could just use leading commas ====
  
 <code php> <code php>
Line 133: Line 148:
 ==== The arguments for & against are weak ==== ==== The arguments for & against are weak ====
  
-TL;DR: +**TL;DR:** 
-For: It's handy. (And the reasons detailed above). + 
-Against: It's ugly. There are only minor gains.+  * **For it:** It's handy. (And the reasons detailed above). 
 +  * **Against:** It's ugly. There are only minor gains.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 143: Line 159:
 ===== Proposed PHP Version ===== ===== Proposed PHP Version =====
  
-PHP 7.1+PHP 7.2
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-Each trailing comma list syntax ​would get its own vote and would require ​a 2/3 majority to pass.+Each trailing comma list syntax ​has its own vote and requires ​a 2/3 majority to pass.
  
-  - Function/​method arguments (declarations & calls) + 
-  ​- ​Grouped namepaces +==== Function/​method arguments (declarations & calls) ​==== 
-  ​- ​Interface implementations on a class + 
-  ​- ​Trait implementations on a class +<code php> 
-  ​- ​Class member lists+// Function/​method arguments (call) 
 +fooCall( 
 +    $arg1, 
 +    $arg2, 
 +    $arg3, 
 +); 
 +     
 +// Function/​method arguments (declaration) 
 +function something( 
 +    FooBarBazInterface $in, 
 +    FooBarBazInterface $out, 
 +) : bool { 
 +
 +</​code>​ 
 + 
 +<doodle title="​Allow trailing commas in function/​method arguments (declarations and calls)"​ auth="​SammyK"​ voteType="​single"​ closed="​true">​ 
 +   * Yes 
 +   * No 
 +</​doodle>​ 
 + 
 +==== Grouped namepaces ​==== 
 + 
 +<code php> 
 +<?php 
 +use Foo\Bar\{ 
 +    Foo, 
 +    Bar, 
 +    Baz, 
 +}; 
 +</​code>​ 
 + 
 +<doodle title="​Allow trailing commas in grouped namepaces"​ auth="​SammyK"​ voteType="​single"​ closed="​true">​ 
 +   * Yes 
 +   * No 
 +</​doodle>​ 
 + 
 +==== Interface implementations on a class ==== 
 + 
 +<code php> 
 +class Foo implements 
 +    FooInterface,​ 
 +    BarInterface,​ 
 +    BazInterface,​ 
 +
 +
 +</​code>​ 
 + 
 +<doodle title="​Allow trailing commas in interface implementations on a class" auth="​SammyK"​ voteType="​single"​ closed="​true">​ 
 +   * Yes 
 +   * No 
 +</​doodle>​ 
 + 
 +==== Trait implementations on a class ==== 
 + 
 +<code php> 
 +class Foo 
 +
 +    use 
 +        FooTrait, 
 +        BarTrait, 
 +        BazTrait, 
 +    ; 
 +
 +</​code>​ 
 + 
 +<doodle title="​Allow trailing commas in trait implementations on a class" auth="​SammyK"​ voteType="​single"​ closed="​true">​ 
 +   * Yes 
 +   * No 
 +</​doodle>​ 
 + 
 +==== Class member lists ==== 
 + 
 +<code php> 
 +class Foo 
 +
 +    const 
 +        A = 1010, 
 +        B = 1021, 
 +        C = 1032, 
 +        D = 1043, 
 +    ; 
 +    protected 
 +        $a = '​foo',​ 
 +        $b = '​bar',​ 
 +        $c = '​baz',​ 
 +    ; 
 +    private 
 +        $blah, 
 +    ; 
 +
 +</​code>​ 
 + 
 +<doodle title="​Allow trailing commas in class member lists" auth="​SammyK"​ voteType="​single"​ closed="​true">​ 
 +   * Yes 
 +   * No 
 +</​doodle>​ 
 + 
 +==== Inheriting variables from the parent scope in anonymous functions ==== 
 + 
 +<code php> 
 +$foo = function ($bar) use ( 
 +    $a, 
 +    $b, 
 +    $c, 
 +) { 
 + // . . .  
 +}; 
 +</​code>​ 
 + 
 +<doodle title="​Allow trailing commas in use lists for anonymous functions"​ auth="​SammyK"​ voteType="​single"​ closed="​true">​ 
 +   * Yes 
 +   * No 
 +</​doodle>​
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
 The [[https://​github.com/​sgolemon/​php-src/​compare/​master...trailing-comma|original patch by Sara Golemon]]. The [[https://​github.com/​sgolemon/​php-src/​compare/​master...trailing-comma|original patch by Sara Golemon]].
rfc/list-syntax-trailing-commas.1446577284.txt.gz · Last modified: 2017/09/22 13:28 (external edit)