rfc:group_use_declarations

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
Next revisionBoth sides next revision
rfc:group_use_declarations [2015/01/30 22:59] – require a trailing T_NS_SEPARATOR on initial proposed syntax marciorfc:group_use_declarations [2015/02/13 17:41] – Use DokuWiki '' syntax ajf
Line 1: Line 1:
 ====== PHP RFC: Group Use Declarations ====== ====== PHP RFC: Group Use Declarations ======
 +  * Version: 0.4
   * Date: 2015-01-28   * Date: 2015-01-28
   * Author: Márcio Almada, marcio.web2@gmail.com   * Author: Márcio Almada, marcio.web2@gmail.com
-  * Status: Draft+  * Status: Voting (previously Under Discussion)
   * First Published at: http://wiki.php.net/rfc/group_use_declarations   * First Published at: http://wiki.php.net/rfc/group_use_declarations
   * Patch: https://github.com/php/php-src/pull/1005   * Patch: https://github.com/php/php-src/pull/1005
Line 23: Line 24:
  
 ===== Proposal ===== ===== Proposal =====
-Group use declarations are just **syntax sugar** to cut verbosity when importing multiple entities from a common namespace. Using common PHP library examples, the following use declarations are equivalents:+Group use declarations are just **syntactic sugar** to cut verbosity when importing multiple entities from a common namespace. Using common PHP library examples, the following use declarations are equivalents:
  
 <code php> <code php>
Line 72: Line 73:
  
 === Non Mixed Use Declarations === === Non Mixed Use Declarations ===
-As expected, non mixed group use declarations are supported:+As expected, non mixed group use declarations are also supported:
  
 <code php> <code php>
Line 89: Line 90:
  
 The current implementation of namespaces does not allow mixed imports of functions, constants and classes. The proposed implementation would allow mixed imports like this: The current implementation of namespaces does not allow mixed imports of functions, constants and classes. The proposed implementation would allow mixed imports like this:
- 
-Current namespace implementation does not allow mixed imports of functions, constants and classes. The proposed implementation would allow mixed imports like this: 
  
 <code php> <code php>
Line 107: Line 106:
 ===== Justification ===== ===== Justification =====
  
-Group use statements makes it easier to identify that multiple imported entities are from the same module. For the first example in this RFC, instead of having to read 'FooLibrary\Bar\Baz\Biz' four times to see that the namespace is the same for each entry, it is clear by the grouping syntax that the entries all come from the same namespace.+Group use declaratations facilitates to import multiple structures from a common namespace and cuts a good level of verbosity in most cases. Group use declaratations makes it easier to identify that multiple imported entities belong to the same module.
  
-Alsogroup use statements makes it easier to see in patch diffs that a dependency on a new module has been introduced. For instance, a patch that adds a new dependency on "Consolidation" namespace has been introduced:+For the first example in this RFCinstead of having to read 'FooLibrary\Bar\Baz\Biz' four times to see that the namespace is the same for each entry, it is clear by the grouping syntax that the entries all come from the same namespace. 
 + 
 +The same advantage regardless readability is noticeable on patch diffs that a dependency on a new module has been introduced. For instance, a patch that adds a new dependency on "Consolidation" namespace has been introduced:
  
 <code> <code>
Line 119: Line 120:
 </code> </code>
  
-With the current use syntax it's necessary to read to the 30th character of the 2nd modified line to realise that the patch adds a new dependency. The proposed syntax is much more readable:+With the current use syntax it's necessary to read to the 30th character of the 2nd modified line to realize that the patch adds a new dependency. The proposed syntax is much more readable:
  
 <code> <code>
Line 131: Line 132:
 </code> </code>
  
 +==== Opinions ====
 +
 +This is a condensed list of unique reactions collected during email discussions and research phase:
 +
 +=== In Favor Or Partially Favorable ===
 +
 +  * It's more readable and makes it easier to import classes, constants and functions in a concise way.
 +  * It seems like a step in the right direction to improving namespaces and more closely approximating the better systems (like Python module imports).
 +  * We really should have a shorthand for condensing use declarations. I wonder if the syntax could feel more "PHP" but maybe I'm just nitpicking.
 +  * The benefits for users which are using IDEs seems marginal, but all other users could actually benefit from it so I'm in favor of the proposal.
 +  * I would like to have it with the option for non enforced trailing "\" (T_NS_SEPARATOR) syntax.
 +  * I'm in favor of the shorthand syntax but I don't care about the nesting option.
 +
 +=> Requests for a syntax variation without a trailing "\" were quite frequent and resulted in a voting [[group_use_declarations#options|option]].\\
 +=> Based on overall feedback the nested namespace option was removed.
 +
 +=== Contrary Or Indifferent ===
 +
 +  * With proper tooling I almost can't remember when I wrote use statements "by hand" so I don't need it.
 +  * The search for literal full qualified namespaces can become less straightforward.
  
 ===== About The Syntax Choice ===== ===== About The Syntax Choice =====
 The syntax chosen is inline with the current [[http://php.net/traits|trait adaptation]] syntax to make it look and feel similar to existing PHP standards. Such design choice allows the feature to stay familiar and intuitive to most PHP user base. The syntax chosen is inline with the current [[http://php.net/traits|trait adaptation]] syntax to make it look and feel similar to existing PHP standards. Such design choice allows the feature to stay familiar and intuitive to most PHP user base.
  
-===== Optional: Nested Group Use Declarations ===== +===== Options =====
-As the author of this RFC, I consider the **nesting** option **too complex** to be introduced. But since many people mentioned this **possibility**, it will be put into the discussion so the community has a chance to appoint.+
  
-**Nesting** would allow the following use case:+==== Trailing T_NS_SEPARATOR ==== 
 +During research phase a syntax variant **without** a trailing "\" (T_NS_SEPARATOR) emerged as a debatable option:
  
 <code php> <code php>
-use Symfony\Component\Console\{ +// with trailing "\" 
-  Helper\Table, + 
-  Input\{ ArrayInputInputInterface +use Foo\Bar\Baz\{ ClassAClassBClassC, ClassD }; 
-  Output\{ NullOutput, OutputInterface }, + 
-  Question\{ QuestionChoiceQuestionConfirmationQuestion } +// vs 
-};+// without trailing "\" 
 + 
 +use Foo\Bar\BazClassAClassBClassC, ClassD };
 </code> </code>
 +
 +This shall be addressed on voting phase: should a trailing "\" be enforced or removed from syntax?
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 159: Line 184:
 === On Backward Compatibility === === On Backward Compatibility ===
 This RFC is backwards compatible with previous PHP releases. It's worth to add that the patch will **not** affect runtime performance. This RFC is backwards compatible with previous PHP releases. It's worth to add that the patch will **not** affect runtime performance.
- 
-===== Open Issues ===== 
-  * Should nested group use declarations be allowed? 
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
 Original namespace implementation is not affected by the addition of group use declarations. Current syntax would still be valid. Original namespace implementation is not affected by the addition of group use declarations. Current syntax would still be valid.
  
-===== Proposed Voting Choices ===== +===== Support in other languages ===== 
-As this RFC represents a language change a two third majority is required+Other languages have similar ways to import multiple entities from a given package or module: 
 + 
 +  * [[http://www.rust-lang.org|Rust Language]] has a very similar syntax ''use a::b::{c, d, e, f};'' 
 +  * [[http://www.scala-lang.org/old/node/119|Scala]] has a very similar syntax ''use a.b.{c, d, e, f};'' 
 +  * [[https://www.python.org|Pyhon]] has a different syntax but with the same objective: ''from fibo import fib, fib2, fib3'' 
 + 
 +===== Votes ===== 
 +As this is a language change, this RFC requires 2/3 majority of Yes votes (with or without trailing "\") to passVoting started on 2015-02-11 and will end on 2015-02-25. 
 + 
 +<doodle title="Should Grouped Use Declarations be added to PHP 7" auth="marcio" voteType="single" closed="false"> 
 +   * Yes - **with** a trailing "\" 
 +   * Yes - **without** a trailing "\" 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
 The implementation aims to be minimal and of easy maintenance. I created a PR so that the patch diff can be viewed easily: https://github.com/php/php-src/pull/1005 The implementation aims to be minimal and of easy maintenance. I created a PR so that the patch diff can be viewed easily: https://github.com/php/php-src/pull/1005
  
-The current initial implementation can be found on my PHP [[https://github.com/marcioAlmada/php-src/tree/group-use|fork]]. You can also directly contribute to this RFC by sending a pull request to https://github.com/marcioAlmada/RFCs.+The current implementation can be found on my PHP [[https://github.com/marcioAlmada/php-src/tree/group-use|fork]]. You can also directly contribute to this RFC by sending a pull request to https://github.com/marcioAlmada/RFCs.
  
 ===== References ===== ===== References =====
 There is no found record related to group use declarations on mailing lists. There is no found record related to group use declarations on mailing lists.
  
-==== Support in other languages ==== +===== Changelog ===== 
-Other languages have similar ways to import multiple entities from given package or module:+ 
 +  * 0.1 - first proposal 
 +  * 0.2 - added trailing "\" syntax to proposal 
 +  * 0.3 - added voting option for non trailing "\" syntax 
 +  * 0.4 - RFC was simplified"nesting" voting option was removed
  
-  * [[http://www.rust-lang.org|Rust Language]] has a very similar sintax `use a::b::{c, d, e, f};` +===== Acknowledgements =====
-  * [[http://www.scala-lang.org/old/node/119|Scala]] has a very similar sintax `use a.b.{c, d, e, f};` +
-  * [[https://www.python.org|Pyhon]] has a different syntax but with the same objective: `from fibo import fib, fib2, fib3`+
  
-===== Rejected Features ===== +Thanks to:
-Awaiting discussion.+
  
 +  - Daniel Ackroyd for reviewing this RFC.
 +  - Andrea and Rangad for their insightful opinions.
 +  - NikiC for providing accurate information about the PHP implementation.
 +  - All people on http://chat.stackoverflow.com/rooms/11/php
rfc/group_use_declarations.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1