Skip to content

[ExpressionLanguage] Feature Null-coalescing operator #16743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

mytuny
Copy link
Contributor

@mytuny mytuny commented Apr 22, 2022

[ExpressionLanguage] [NEW FEATURE]

This PR introduces the support for null-coalescing operator ??.

The proposed change, includes a paragraph under the sub-page /expression_language/syntax to describe the new feature usage. The sub-paragraph has a title of Null-coalescing operator under the paragraph Supported Operators.

The actual work related to this Doc PR available as Symfony PR .

@carsonbot carsonbot added this to the 6.1 milestone Apr 22, 2022
@wouterj wouterj modified the milestones: 6.1, next May 1, 2022
@wouterj wouterj added the Waiting Code Merge Docs for features pending to be merged label May 1, 2022
fabpot added a commit to symfony/symfony that referenced this pull request Jul 25, 2022
…yntax (mytuny)

This PR was merged into the 6.2 branch.

Discussion
----------

[ExpressionLanguage] Add support for null coalescing syntax

| Q             | A
| ------------- | ---
| Branch?       | 6.1
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #45411, #21691
| License       | MIT
| Doc PR        | symfony/symfony-docs#16743

This is another waited feature for the syntax of the expression-language component. The [null-coalescing](https://wiki.php.net/rfc/isset_ternary) operator ``??`` becomes a need for variant programming needs these days.

Following my previous PR introducing the null-safe operator (#45795). I'm hereby introducing yet another essential operator to make the syntax even more complete.

The null-coalescing operator is a syntactic sugar for a common use of ternary in conjunction with ``isset()`` (in PHP) or equivalent in other languages. This is such a common use-case to the point that almost all majors programming syntax nowadays support a sort of a short-hand for that operation namely coalescing operator. Now it's time for the syntax of Expression-Language to do so!

Expressions like:

* ``foo.bar ?? 'default'``
* ``foo[3] ?? 'default'``
* ``foo.bar ?? foo['bar'] ?? 'default'``

will default to the expression in the right-hand-side of the ``??`` operator whenever the expression in the left-hand-side of it does not exist or it's ``null``. Note that this coalescing behavior can be chained and the validation logic takes decreasing priority from left to right.

Commits
-------

8e3c505 [ExpressionLanguage] Add support for null coalescing syntax
@javiereguiluz javiereguiluz added Status: Reviewed and removed Status: Needs Review Waiting Code Merge Docs for features pending to be merged labels Jul 29, 2022
@javiereguiluz javiereguiluz modified the milestones: next, 6.2 Jul 29, 2022
@javiereguiluz javiereguiluz merged commit 3cc1163 into symfony:6.1 Jul 29, 2022
@javiereguiluz
Copy link
Member

Sofien, thanks a lot for contributing this nice feature and its docs ... and congrats on your first Symfony Docs contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants