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

[Expression Language] Null coalescing operator doesn't work? #54359

Closed
adamkiss opened this issue Mar 21, 2024 · 6 comments · May be fixed by #54757
Closed

[Expression Language] Null coalescing operator doesn't work? #54359

adamkiss opened this issue Mar 21, 2024 · 6 comments · May be fixed by #54757

Comments

@adamkiss
Copy link

adamkiss commented Mar 21, 2024

Symfony version(s) affected

7.0

Description

According to docs, we can use null-coalescing operator to provide fallback to potentially empty or even nonexistent variables. However I can't make it work, even in a simple file with nothing but Expression Language.

How to reproduce

$lang = new ExpressionLanguage();

$data = [
    'exists' => true,
];

$expression1 = 'notexists ?? "nope,"';
$expression2 = '(notexists ?? "nope,")';
$expression3 = '(notexists ?? false) ? "yes" : "no"';

foreach ([$expression1, $expression2, $expression3] as $exp) {
    try {
        var_dump($lang->evaluate($exp, $data));
    } catch (\Throwable $th) {
        echo('!');
    }
}
// expected: nope,nope,no
// actual: !!!

(In the reproduction I have suppressed the throws, but every evaluate fails with Fatal error: Uncaught Symfony\Component\ExpressionLanguage\SyntaxError: Variable "notexists" is not valid around position …

Possible Solution

No response

Additional Context

No response

@MatTheCat
Copy link
Contributor

The problem is not the operator but the fact you cannot reference variables you haven’t passed in an expression. What is your use-case for doing so?

@adamkiss
Copy link
Author

adamkiss commented Mar 21, 2024

@MatTheCat Acting on a variable hashmap

If it's not possible, I'll understand, but docs specifically mention this usecase: https://symfony.com/doc/current/components/expression_language.html#null-coalescing-operator

Right first example is foo ?? "no", and the null-coalescing description is

It returns the left hand-side if it exists and it's not null; otherwise it returns the right hand-side.

(emphasis mine)

@MatTheCat
Copy link
Contributor

Indeed, but looking at the code it seems this use-case was never intended (the example will only work if foo is null) 🤔

Maybe you could open an issue in symfony-docs? It seems also weird to have the null-coalescing operator documented here instead of here.

@adamkiss
Copy link
Author

@MatTheCat I'm not using it as a part of Symfony, so I wasn't sure where to post it; Will post it in docs

@xabbuh
Copy link
Member

xabbuh commented Mar 21, 2024

closing in favour of the docs issue

@xabbuh xabbuh closed this as not planned Won't fix, can't repro, duplicate, stale Mar 21, 2024
@adamkiss
Copy link
Author

@xabbuh I hoped for "that should work" tbh 😂

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

Successfully merging a pull request may close this issue.

4 participants