Skip to content

[ExpressionLanguage][FEATURE] Add support for Nullsafe syntax #7

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

Closed
wants to merge 5 commits into from

Conversation

mytuny
Copy link
Contributor

@mytuny mytuny commented Mar 20, 2022

This is a long-time-lasted feature for the ExpressionLanguage component. I've been waiting for the support of Nullsafe operator in expressions dealing with mutable objects, until I finally decided to work on it once for all 👍

The lack of nullsafety feature has been repeatedly reported as a BUG several time (e.g #45411 & #21691) when it is actually a missing feature.

Currently, expressions like foo.bar assumes that the property bar "always" exists on the object foo and if doesn't the parser throws a RuntimeException. Although, sometimes, that's exactly the behavior we need, some other times we may work with mutable objects with uncontrolled structure, thus, such assumption is error-prone and will force adding extra checks making the expression uglier and less readable.

The proposed work, introduces the support for the ?. syntax alongside with the usual . syntax to help working with objects with dynamic structure. The two notations works identically in all normal cases. The difference occurs when trying to access non-existant properties and/or methods where the . notation will throw a RuntimeException as usual and the ?. notation will return null instead and no errors nor exceptions will be thrown. Hence the name "Null-Safe".

PS: This work account ONLY for accessing object's properties and methods. It does not account for non-existant array items which is a seperate problem that can be addressed by introducing the null coalescing operator. Another feature that I'm currently working on as well 💯

@symfony-bot
Copy link

symfony-bot bot commented Mar 20, 2022

Thanks for your pull request! We love contributions.

However, this repository is what we call a "subtree split": a read-only copy of one directory of the main Symfony repository. It is used by Composer to allow developers to depend on specific Symfony components.

If you want to contribute, you should instead open a pull request on the main repository:

https://github.com/symfony/symfony

Thank you for your contribution!

PS: if you haven't already, please add tests, and beware that bug fixes should be submitted on the lowest maintained branch where they apply.

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

Successfully merging this pull request may close these issues.

2 participants