rfc:group_use_declarations
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:group_use_declarations [2015/02/01 12:45] – add missing comma marcio | rfc:group_use_declarations [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
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: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
* Patch: https:// | * Patch: https:// | ||
Line 23: | Line 24: | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | Group use declarations are just **syntax sugar** | + | Group use declarations are used 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 105: | Line 106: | ||
===== Justification ===== | ===== Justification ===== | ||
- | Group use statements | + | 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 |
- | Also, group use statements makes it easier | + | For the first example in this RFC, instead of having to read ' |
+ | |||
+ | 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 " | ||
< | < | ||
Line 117: | Line 120: | ||
</ | </ | ||
- | With the current use syntax it's necessary to read to the 30th character of the 2nd modified line to realise | + | With the current use syntax it's necessary to read to the 30th character of the 2nd modified line to realize |
< | < | ||
use Symfony\Component\Console\Question\{ | use Symfony\Component\Console\Question\{ | ||
ConfirmationQuestion, | ConfirmationQuestion, | ||
- | ChoiceQuestion as Choice; | + | ChoiceQuestion as Choice, |
+ | + | ||
Question, | Question, | ||
Line 129: | Line 132: | ||
</ | </ | ||
+ | ==== Opinions ==== | ||
- | ===== About The Syntax Choice | + | 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 " | ||
+ | * 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 " | ||
+ | * 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 " | ||
+ | => 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 ==== | ||
The syntax chosen is inline with the current [[http:// | The syntax chosen is inline with the current [[http:// | ||
- | ===== 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**, | + | |
- | **Nesting** would allow the following use case: | + | ==== Trailing T_NS_SEPARATOR ==== |
+ | During research phase a syntax variant | ||
<code php> | <code php> | ||
- | use Symfony\Component\Console\{ | + | // with trailing " |
- | Helper\Table, | + | |
- | Input\{ ArrayInput, InputInterface }, | + | use Foo\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD }; |
- | | + | |
- | | + | // vs |
- | }; | + | // without trailing "\" |
+ | |||
+ | use Foo\Bar\Baz{ ClassA, ClassB, ClassC, ClassD | ||
</ | </ | ||
- | ===== Backward Incompatible Changes ===== | + | This shall be addressed on voting phase: should a trailing " |
- | There is no BC breaks with current implementation | + | |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 154: | Line 178: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
+ | |||
+ | === On Functional Programming / Autoloading === | ||
+ | |||
+ | Group use declarations is also very helpful for the PHP user base more fond to the **functional** programming paradigms. This RFC would be especially important if PHP finally gets **function autoloading**. | ||
+ | |||
+ | Currently, sets of independent functions have to be implemented as static classes. But with function autoloading, | ||
+ | |||
+ | <code php> | ||
+ | // Current use syntax: | ||
+ | |||
+ | use function iter\range; | ||
+ | use function iter\map; | ||
+ | use function iter\filter; | ||
+ | use function iter\apply; | ||
+ | use function iter\reduce; | ||
+ | use function iter\fn\operator; | ||
+ | |||
+ | // Proposed syntax: | ||
+ | |||
+ | use function iter\{ range, map, filter, apply, reduce, fn\operator }; | ||
+ | |||
+ | </ | ||
=== 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 | + | Other languages have similar ways to import multiple entities from a given package or module: |
+ | |||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | |||
+ | ===== Votes ===== | ||
+ | As this is a language change, this RFC requires | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes - **with** a trailing " | ||
+ | * Yes - **without** a trailing " | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ===== Discussion in a Nutshell ===== | ||
+ | |||
+ | In case you missed the discussion. This section lists the common counter points towards this RFC and how they were replied by the RFC author and supporters on mailing lists and media: | ||
+ | |||
+ | ==== My IDE already does the imports for me and folds the namespaces so I don't need this feature ==== | ||
+ | |||
+ | We should create **usable languages by design** rather than **languages only usable by tooling**. Tooling can be very important | ||
+ | but the moment you notice a stack of tools is needed to have a reasonable experience and readability with a programming language then something needs to be improved. | ||
+ | |||
+ | A programming language should be able stand on its own and not require IDE assistance for reasonable use. That's what this RFC does, it brings a very common feature already available in many languages employing some kind of namespacing. | ||
+ | |||
+ | ==== The RFC encourages violation of the the SRP and other best practices ==== | ||
+ | |||
+ | This is **not true**. Nikita already wrote a great response to this on the official PHP mailing list. Beware of the slay of good argumentation ahead: http:// | ||
+ | |||
+ | ==== We should have "first class packages" | ||
+ | |||
+ | This is painfully illogic. Group use declarations and "first class packages" | ||
+ | |||
+ | Doing so **prior** to a major PHP7 **release** is, in fact, the **best hour** to **approve** this RFC. | ||
+ | |||
+ | ==== We should go for Python' | ||
+ | |||
+ | I doubt Python syntax will ever be proposed for PHP because it would require the the **from** keyword to be reserved, hence one of the reasons it was discarded during research and discussion. Other reason is that the glob brace syntax is more tailored for PHP and it is a [[https:// | ||
===== 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:// | The implementation aims to be minimal and of easy maintenance. I created a PR so that the patch diff can be viewed easily: https:// | ||
- | The current | + | The current implementation can be found on my PHP [[https:// |
===== 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 a given package or module: | + | |
+ | * 0.1 - first proposal | ||
+ | * 0.2 - added trailing " | ||
+ | * 0.3 - added voting option for a non trailing " | ||
+ | * 0.4 - RFC was simplified: " | ||
+ | |||
+ | ===== Acknowledgements ===== | ||
- | * [[http:// | + | Thanks to: |
- | * [[http:// | + | |
- | * [[https:// | + | |
- | ===== Rejected Features ===== | + | - Daniel Ackroyd for reviewing this RFC. |
- | Awaiting discussion. | + | - Andrea and Rangad for their insightful opinions. |
+ | - NikiC for providing accurate information about the PHP implementation. | ||
+ | - All people on http:// | ||
rfc/group_use_declarations.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1