Skip to content
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

[ExpressionLanguage] Null-Coalescing operator supporting non-existent data #19695

Closed
adamkiss opened this issue Mar 21, 2024 · 3 comments
Closed
Labels
ExpressionLanguage hasPR A Pull Request has already been submitted for this issue.
Milestone

Comments

@adamkiss
Copy link

adamkiss commented Mar 21, 2024

Potential doc issue for symfony/symfony#54359

Docs suggest that Expression Language supports null-coalescing operator for variables not passed in:

$data = ['exists' => 1];
$language->evaluate('doesnt_exist ?? 2', $data);

But it was suggested to me in the linked issue that the code doesn't seem to actually support that, and that null coalescing operator also isn't mentioned in https://symfony.com/doc/current/reference/formats/expression_language.html#supported-operators

@stof
Copy link
Member

stof commented Mar 21, 2024

To be exact, the if it exists part is true when accessing potentially-missing array keys. But accessing a non-existent variable won't be allowed even with this operator (side note: this is also the way this operator works in JS, where a ?? 'fallback' will still throw a ReferenceError if the variable a does not exist)

@adamkiss
Copy link
Author

adamkiss commented Mar 21, 2024

@stof It specifically calls out "like PHP null-coalescing", where $never_defined ?? 'fallback' is perfectly valid thing.

@stof
Copy link
Member

stof commented Mar 21, 2024

Yeah, this part should be updated. I'm not saying that the doc is correct right now. But the part saying if it exists must not be removed entirely either.

@OskarStark OskarStark changed the title [Expression-Language] Null-Coalescing operator supporting non-existent data [ExpressionLanguage] Null-Coalescing operator supporting non-existent data Mar 24, 2024
@javiereguiluz javiereguiluz added the hasPR A Pull Request has already been submitted for this issue. label Mar 26, 2024
@javiereguiluz javiereguiluz added this to the 6.4 milestone Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ExpressionLanguage hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

4 participants