Short Closures 2.0

Read the RFC
101
373 yes
66 no

This RFC proposes a way to have multi-line short closures — closures that don't need explicit use statements, but can still have multiple lines and a return statement.

Click the bar to cast your vote!
85%
15%
3

Classic closure is perfect for this kind of use case

Share:
degraciamathieu avatar
degraciamathieu
voted yes
2

I don't understand "no" voters arguing about readability when use (...) is the one statement decreasing readability.

Voted "yes" because I think the opposite :

fn ($something) {
	return $somethingFromOuterScope . $something;
}

Is easy to read... $somethingFromOuterScope can not come from anywhere but only from the most close outer scope...

Share:
trehinos avatar
trehinos
voted yes
2

A lot of posts point out that JavaScript's syntax matches what's proposed here. If PHP remains a hold-out by not adding this syntax, it feeds the reputation that PHP is always behind the curve.

Marketability aside, i get sick of writing anonymous functions with use () statements where i need to make changes to an imported variable, and thus have to import by-reference. It's clunky, especially when references are so frowned-upon. To me it's much cleaner to have a short-closure where i know all data in scope remains in the same scope of that function.

Share:
thookerov avatar
thookerov
voted yes
2

I don't see the advantage. Use short closures for oneliners but this rfc makes it complicated and messy

Share:
uli avatar
uli
voted no
2

I never understood the limit of one statement in the short arrow function syntax.

This seems to fix it and put arrow functions back on the level of the standard anonymous function.

Share:
agc avatar
agc
voted yes
2

So many times I had to drop array_map/reduce or similar functions just because I didn't want to use "classic" closure syntax. Honestly, I don't even see a big need in having the "classic" syntax nowadays, but having a multiline new-style closure looks very logical to me.

Share:
ozahorulia avatar
ozahorulia
voted yes
2

The function scope must be respected in all cases. Function without scopes and default "use all variables" would be confusing. If you have nested closures, where the scope ends. Closures would be a nightmare like JavaScript functions.

Share:
anibalsanchez avatar
anibalsanchez
voted no
2

The use statements are usually what is keeping me from using the short closures, so having this resolved would make life a lot easier in those cases and the code cleaner

Share:
Carakas avatar
Carakas
voted yes
2

Definitely will make our lives much more easier.

Share:
tiago avatar
tiago
voted yes
1

Would make it more convenient to pass callbacks to functions when the callback needs the local scope.

Share:
daniel avatar
daniel
voted yes
1

I find myself reaching for foreach, while, and for loops, to do things that PHP already provided in functions, simply because using arrow functions would look too messy.

Share:
nathan avatar
nathan
voted yes
1

It improves readability

Share:
aamirsohailkmas avatar
aamirsohailkmas
voted yes
1

.

Share:
cverond avatar
cverond
voted yes
1

Just why not :)) No one was forced to use them, but when it's introduce it has many useful applications.

Share:
arziel avatar
arziel
voted yes
1

Short closures are definitely needed!

Share:
lkankowski avatar
lkankowski
voted yes

Check out another RFCs

new MyClass()->method() without parentheses

Chain method on newly created objects without parentheses

35
46 yes
13 no
The Pipe Operator

The "pipe operator" |> allows you to chain multiple function calls in a more convenient way.

90
262 yes
122 no
Property Hooks

A new way of interacting with properties

64
122 yes
78 no
RSS Feed Contribute Watch on YouTube Our License
© 2024 RFC Vote. This project is open source. Contribute and collaborate with us!