Skip to content

[ExpressionLanguage] Add support for null coalescing syntax #46142

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

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 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.

@carsonbot carsonbot added this to the 6.1 milestone Apr 22, 2022
@carsonbot carsonbot changed the title [ExpressionLanguage] [Feature] Add support for null coalescing syntax [ExpressionLanguage] Add support for null coalescing syntax Apr 22, 2022
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

@fabpot fabpot modified the milestones: 6.1, 6.2 May 8, 2022
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(please apply fabbot's path)

@nicolas-grekas nicolas-grekas force-pushed the feature/expression-language/null-coalescing branch from 825d77f to faf40df Compare May 10, 2022 08:51
@nicolas-grekas
Copy link
Member

Can you please rebase? This conflicts after #46454
Please also double check that the issue fixed in #46454 didn't happen here just in case.

@kvailas
Copy link

kvailas commented Jul 13, 2022

Hello @nicolas-grekas , many many many thanks for working on this one!
Is there a plan to release the new feature within the month or so?
It would be great to know!

@fabpot
Copy link
Member

fabpot commented Jul 20, 2022

@kvailas See https://symfony.com/doc/current/contributing/community/releases.html for more information about our release process.
If this PR is merged in the next couple of months, it will be part of 6.2.

@kvailas
Copy link

kvailas commented Jul 20, 2022

@fabpot actually i am using the expression language library as part of a non-symfony based project. Just loading the library in order to use the expression language from its respective repo.

Thanks for your input, appreciated.

@stof
Copy link
Member

stof commented Jul 20, 2022

@kvailas this does not change when the releases of the components happen (btw, Symfony-based projects also download components from the subtree-split repos)

@fabpot
Copy link
Member

fabpot commented Jul 20, 2022

Can you rebase on current 6.2?

@fabpot
Copy link
Member

fabpot commented Jul 25, 2022

Thank you @mytuny.

@fabpot fabpot force-pushed the feature/expression-language/null-coalescing branch from faf40df to 8e3c505 Compare July 25, 2022 12:31
@fabpot fabpot merged commit 39191b4 into symfony:6.2 Jul 25, 2022
@fabpot
Copy link
Member

fabpot commented Jul 25, 2022

FYI, follow-up PR here: #47058

@fabpot fabpot mentioned this pull request Oct 24, 2022
}
};

yield ['foo.bar ?? "default"', null];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yield foo ?? 'default' <- will be red

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.

Expression Language package: doesn't handle the absence of the property and an error displays
7 participants