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

Arrow function are not allowed outside filters #3192

Closed
alex-dev opened this issue Nov 11, 2019 · 3 comments · Fixed by #4378
Closed

Arrow function are not allowed outside filters #3192

alex-dev opened this issue Nov 11, 2019 · 3 comments · Fixed by #4378

Comments

@alex-dev
Copy link

Arrow functions currently can only be used in a filter as per ExpressionParser.
This prevents multiple functionalities such as

  • passing arrows to an include context so it can forward it to an actual filter;
  • passing arrows to a macro or a function;
  • defining and setting a callable so it can be used in subsequent expressions;
@fabpot
Copy link
Contributor

fabpot commented Nov 11, 2019

The limitations you list are on purpose to limit usage of arrow functions to simple use cases.

@khalwat
Copy link

khalwat commented Aug 2, 2022

To make this work this one line: https://github.com/twigphp/Twig/blob/3.x/src/ExpressionParser.php#L483 from:

                    foreach ($this->parseArguments() as $n) {

to:

                    foreach ($this->parseArguments(false, false, true) as $n) {

then closures all work in the context we want, e.g.:

    {% set collection = collect(['a', 'b', 'c']) %}
    {% set contains = collection.contains((value, key) => value == 'a') %}

with a nice, clean, normal syntax. I'd love to see this happen, too.

@fabpot
Copy link
Contributor

fabpot commented Oct 4, 2024

See #4378

@fabpot fabpot closed this as completed in 9b35289 Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants