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

How to use arrow functions as callable? #3402

Closed
I-Valchev opened this issue Sep 30, 2020 · 10 comments · Fixed by #4378
Closed

How to use arrow functions as callable? #3402

I-Valchev opened this issue Sep 30, 2020 · 10 comments · Fixed by #4378

Comments

@I-Valchev
Copy link

I'd be really great if Twig arrow functions can be passed as arguments.

e.g. Laravel collections unique method accepts a callable.

I'd like to do something like this:

{{ mycollection.unique(item => item.id) }}

Is that possible? Or an RFC?

@fabpot
Copy link
Contributor

fabpot commented Oct 6, 2020

It's not supported. Arrow functions are limited in scope.

@fabpot fabpot closed this as completed Oct 6, 2020
@I-Valchev
Copy link
Author

@fabpot okay... but why not keep this is an RFC?

It would be very useful to have more flexibility to filter/sort arrays or collection objects.

@timkelty
Copy link

I'd love to see this as well! Specifically, would use here: https://github.com/timkelty/craftcms-classmate#arrow-functions

@michaelrog
Copy link

michaelrog commented Sep 3, 2021

@fabpot Would you consider making $allowArrow configurable, i.e. opt-in on a per-Environment basis?

The ability to pass tiny little closures to Collection methods would be really nice. I definitely appreciate the spirit of limiting arrow functions to narrow usage, but the Collections use case is very compelling.

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

@brandonkelly
Copy link
Contributor

Would really love to see this as well. When writing templates, it’s really confusing why arrow functions work in some cases but not others, when you know the underlying method accepts a callable.

@khalwat
Copy link

khalwat commented Aug 7, 2022

Here's a solution for Craft CMS: https://github.com/nystudio107/craft-closure

@tbreuss
Copy link

tbreuss commented Jan 14, 2023

Would really love to see this as well.

@khalwat
Copy link

khalwat commented Feb 2, 2024

Anyone who is interested in this, I added it here: #3951 (comment)

@fabpot fabpot reopened this Oct 4, 2024
@fabpot
Copy link
Contributor

fabpot commented Oct 4, 2024

See #4378

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.

7 participants