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 13:34] – add T_NS_SEPARATOR variant to open issues 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 ====== |
- | * Date: 2015-01-28 | + | * Version: 0.4 |
- | * Author: Márcio Almada, marcio.web2@gmail.com | + | |
- | * Status: | + | * Author: Márcio Almada, marcio.web2@gmail.com |
- | * First Published at: http:// | + | * Status: |
- | * Patch: https:// | + | * First Published at: http:// |
+ | * Patch: https:// | ||
- | | + | ===== Introduction ===== |
- | This RFC aims to improve current PHP namespace implementation by introducing the concept of **Group Use Declarations**: | + | This RFC aims to improve current PHP namespace implementation by introducing the concept of **Group Use Declarations**: |
- | | + | <code php> |
- | // Proposed group use syntax: | + | // Proposed group use syntax: |
- | | + | use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo }; |
- | | + | // Compared to current use syntax: |
- | | + | use FooLibrary\Bar\Baz\ClassA; |
- | use FooLibrary\Bar\Baz\ClassB; | + | use FooLibrary\Bar\Baz\ClassB; |
- | use FooLibrary\Bar\Baz\ClassC; | + | use FooLibrary\Bar\Baz\ClassC; |
- | use FooLibrary\Bar\Baz\ClassD as Fizbo; | + | use FooLibrary\Bar\Baz\ClassD as Fizbo; |
- | </ | + | </ |
- | | + | ===== 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> |
- | // Current use syntax: | + | // Current use syntax: |
- | | + | use Doctrine\Common\Collections\Expr\Comparison; |
- | use Doctrine\Common\Collections\Expr\Value; | + | use Doctrine\Common\Collections\Expr\Value; |
- | use Doctrine\Common\Collections\Expr\CompositeExpression; | + | use Doctrine\Common\Collections\Expr\CompositeExpression; |
- | | + | // Proposed group use syntax: |
- | | + | use Doctrine\Common\Collections\Expr\{ Comparison, Value, CompositeExpression }; |
- | </ | + | </ |
- | | + | === Compound Namespaces === |
- | Compound namespaces are also allowed. For instance, the following use declarations are equivalents: | + | Compound namespaces are also allowed. For instance, the following use declarations are equivalents: |
- | | + | <code php> |
- | // Current use syntax: | + | // Current use syntax: |
- | | + | use Symfony\Component\Console\Helper\Table; |
- | use Symfony\Component\Console\Input\ArrayInput; | + | use Symfony\Component\Console\Input\ArrayInput; |
- | use Symfony\Component\Console\Output\NullOutput; | + | use Symfony\Component\Console\Output\NullOutput; |
- | use Symfony\Component\Console\Question\Question; | + | use Symfony\Component\Console\Question\Question; |
- | use Symfony\Component\Console\Input\InputInterface; | + | use Symfony\Component\Console\Input\InputInterface; |
- | use Symfony\Component\Console\Output\OutputInterface; | + | use Symfony\Component\Console\Output\OutputInterface; |
- | use Symfony\Component\Console\Question\ChoiceQuestion as Choice; | + | use Symfony\Component\Console\Question\ChoiceQuestion as Choice; |
- | use Symfony\Component\Console\Question\ConfirmationQuestion; | + | use Symfony\Component\Console\Question\ConfirmationQuestion; |
- | | + | // Proposed group use syntax: |
- | | + | use Symfony\Component\Console\{ |
- | Helper\Table, | + | Helper\Table, |
- | Input\ArrayInput, | + | Input\ArrayInput, |
- | Input\InputInterface, | + | Input\InputInterface, |
- | Output\NullOutput, | + | Output\NullOutput, |
- | Output\OutputInterface, | + | Output\OutputInterface, |
- | Question\Question, | + | Question\Question, |
- | Question\ChoiceQuestion as Choice, | + | Question\ChoiceQuestion as Choice, |
- | Question\ConfirmationQuestion, | + | Question\ConfirmationQuestion, |
- | }; | + | }; |
- | </ | + | </ |
- | | + | This is also a real use case. Check out the Laravel source code: |
- | | + | |
- | * [[https:// | + | * [[https:// |
- | | + | === Non Mixed Use Declarations === |
- | As expected, non mixed group use declarations are also supported: | + | As expected, non mixed group use declarations are also supported: |
- | | + | <code php> |
- | // Current use syntax: | + | // Current use syntax: |
- | | + | use function foo\math\sin, |
- | use const foo\math\PI, | + | use const foo\math\PI, |
- | | + | // Proposed non mixed group use syntax: |
- | | + | use function foo\math\{ sin, cos, cosh }; |
- | use const foo\math\{ PI, E, GAMMA, GOLDEN_RATIO }; | + | use const foo\math\{ PI, E, GAMMA, GOLDEN_RATIO }; |
- | </ | + | </ |
- | | + | === Mixed Use Declarations === |
- | | + | The current implementation of namespaces does not allow mixed imports of functions, constants and classes. The proposed implementation would allow mixed imports like this: |
- | | + | <code php> |
- | // Current use syntax: | + | // Current use syntax: |
- | | + | use foo\math\Math; |
- | use const foo\math\PI; | + | use const foo\math\PI; |
- | use function foo\math\sin, | + | use function foo\math\sin, |
- | | + | // Proposed mixed group use syntax: |
- | | + | use foo\math\{ Math, const PI, function sin, function cos, function cosh }; |
- | | + | </ |
- | | + | ===== Justification ===== |
- | | + | 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 "Consolidation" namespace has been introduced: |
- | use Symfony\Component\Console\Question\ConfirmationQuestion; | + | |
- | use Symfony\Component\Console\Question\ChoiceQuestion as Choice; | + | |
- | +use Symfony\Component\Console\Question\OptionQuestion; | + | |
- | +use Symfony\Component\Consolidation\Question\OptionQuestion; | + | |
- | use Symfony\Component\Console\Question\Question; | + | |
- | </ | + | |
- | With the current | + | < |
+ | use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
+ | use Symfony\Component\Console\Question\ChoiceQuestion as Choice; | ||
+ | +use Symfony\Component\Console\Question\OptionQuestion; | ||
+ | +use Symfony\Component\Consolidation\Question\OptionQuestion; | ||
+ | use Symfony\Component\Console\Question\Question; | ||
+ | </ | ||
- | < | + | With the current |
- | | + | |
- | ConfirmationQuestion, | + | |
- | ChoiceQuestion as Choice; | + | |
- | + | + | |
- | Question, | + | |
- | }; | + | |
- | +use Symfony\Component\Consolidation\Question\OptionQuestion; | + | |
- | </ | + | |
+ | < | ||
+ | use Symfony\Component\Console\Question\{ | ||
+ | ConfirmationQuestion, | ||
+ | ChoiceQuestion as Choice, | ||
+ | + | ||
+ | Question, | ||
+ | }; | ||
+ | +use Symfony\Component\Consolidation\Question\OptionQuestion; | ||
+ | </ | ||
- | | + | ==== Opinions |
- | The syntax chosen is inline with the current [[http:// | + | |
- | ===== Options ===== | + | This is a condensed list of unique reactions collected during email discussions and research phase: |
- | | + | === In Favor Or Partially Favorable |
- | During research phase a syntax variant **without** an enforced T_NS_SEPARATOR emerged as a debatable option: | + | |
- | | + | |
- | | + | |
+ | * 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 | ||
+ | * I'm in favor of the shorthand syntax but I don't care about the nesting option. | ||
- | use Foo\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD }; | + | => Requests for a syntax variation without a trailing "\" were quite frequent and resulted in a voting [[group_use_declarations# |
+ | => Based on overall feedback the nested namespace option was removed. | ||
- | // vs | + | === Contrary Or Indifferent === |
- | // without trailing " | + | |
- | use Foo\Bar\Baz{ ClassA, ClassB, ClassC, ClassD }; | + | |
- | | + | |
- | This shall be addressed on discussion phase: should a trailing " | + | ==== About The Syntax Choice ==== |
+ | The syntax chosen is inline with the current [[http:// | ||
- | | + | ===== Options ===== |
- | As the author of this RFC, I consider the **nesting** option **too complex** to be introduced. But since many people mentioned this **possibility**, | + | |
- | | + | ==== Trailing T_NS_SEPARATOR ==== |
+ | During research phase a syntax variant | ||
- | | + | <code php> |
- | use Symfony\Component\Console\{ | + | // with trailing "\" |
- | Helper\Table, | + | |
- | Input\{ ArrayInput, InputInterface }, | + | |
- | Output\{ NullOutput, OutputInterface }, | + | |
- | Question\{ Question, ChoiceQuestion, | + | |
- | }; | + | |
- | </ | + | |
- | This shall be addressed on discussion phase: should nested group use declarations be allowed? | + | use Foo\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD }; |
+ | // vs | ||
+ | // without trailing " | ||
- | ===== Backward Incompatible Changes ===== | + | use Foo\Bar\Baz{ ClassA, ClassB, ClassC, ClassD }; |
- | There is no BC breaks with current implementation or feature concepts. | + | </ |
- | ===== Proposed PHP Version(s) ===== | + | This shall be addressed on voting phase: should a trailing " |
- | | + | |
- | | + | ===== Proposed PHP Version(s) |
+ | This is proposed for the next PHP x, which at the time of this writing would be PHP 7. | ||
- | | + | ===== RFC Impact ===== |
- | This RFC is backwards compatible with previous PHP releases. It's worth to add that the patch will **not** affect runtime performance. | + | |
- | | + | === On Functional Programming / Autoloading |
- | * Should nested group use declarations be allowed? | + | |
- | * Should trailing " | + | |
- | ===== Unaffected PHP Functionality ===== | + | Group use declarations |
- | Original namespace implementation is not affected by the addition of group use declarations. | + | |
- | ===== Proposed Voting Choices ===== | + | Currently, sets of independent functions have to be implemented as static classes. But with function autoloading, |
- | As this RFC represents a language change a two third majority | + | |
- | ===== Patches and Tests ===== | + | <code php> |
- | 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/ | + | // Current use syntax: |
- | The current initial implementation can be found on my PHP [[https:// | + | 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; | ||
- | ===== References ===== | + | // Proposed syntax: |
- | There is no found record related to group use declarations on mailing lists. | + | |
- | ==== Support in other languages ==== | + | use function iter\{ range, map, filter, apply, reduce, fn\operator }; |
- | Other languages have similar ways to import multiple entities from a given package or module: | + | |
- | * [[http:// | + | </code> |
- | * [[http:// | + | |
- | * [[https:// | + | |
- | | + | === 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. |
+ | |||
+ | ===== Unaffected PHP Functionality ===== | ||
+ | Original namespace implementation is not affected by the addition of group use declarations. Current syntax would still be valid. | ||
+ | |||
+ | ===== Support in other languages ===== | ||
+ | Other languages have similar ways to import multiple entities from a given package or module: | ||
+ | |||
+ | | ||
+ | * [[http:// | ||
+ | * [[https:// | ||
+ | |||
+ | ===== Votes ===== | ||
+ | As this is a language change, this RFC requires a 2/3 majority of Yes votes (with or without trailing " | ||
+ | |||
+ | <doodle title=" | ||
+ | * Yes - **with** a trailing " | ||
+ | * Yes - **without** a trailing " | ||
+ | * No | ||
+ | </ | ||
+ | |||
+ | ===== Discussion in a Nutshell ===== | ||
+ | |||
+ | In case you missed the discussion. | ||
+ | |||
+ | ==== 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 ===== | ||
+ | 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 implementation can be found on my PHP [[https:// | ||
+ | |||
+ | ===== References ===== | ||
+ | There is no found record related to group use declarations on mailing lists. | ||
+ | |||
+ | ===== Changelog ===== | ||
+ | |||
+ | * 0.1 - first proposal | ||
+ | * 0.2 - added trailing " | ||
+ | * 0.3 - added voting option for a non trailing " | ||
+ | * 0.4 - RFC was simplified: " | ||
+ | |||
+ | ===== Acknowledgements ===== | ||
+ | |||
+ | Thanks to: | ||
+ | |||
+ | - 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:// | ||
rfc/group_use_declarations.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1