-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Comments
It's not supported. Arrow functions are limited in scope. |
@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. |
I'd love to see this as well! Specifically, would use here: https://github.com/timkelty/craftcms-classmate#arrow-functions |
@fabpot Would you consider making 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. |
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. |
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. |
Here's a solution for Craft CMS: https://github.com/nystudio107/craft-closure |
Would really love to see this as well. |
Anyone who is interested in this, I added it here: #3951 (comment) |
See #4378 |
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:
Is that possible? Or an RFC?
The text was updated successfully, but these errors were encountered: