-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add short closures / arrow functions #3941
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
Conversation
@nikic Will this syntax be allowed: public class MyClass
{
private static int $i = 0;
public static increment(): int => ++self::$i;
} ? |
@carusogabriel If I am correct, this short syntax is for closures only and has nothing to do with methods. https://github.com/php/php-src/pull/3941/files#diff-7eff82c2c5b45db512a9dc49fb990bb8R170 Based on this you can see that it recognises |
@carusogabriel No, this is not supported. It's listed as a possible future extension though: https://wiki.php.net/rfc/arrow_functions_v2#allow_arrow_notation_for_real_functions |
Arrow functions are ternary operators to functions. While they are nice and shorten, they can be hard to read at times; considerably to people who aren't used to them which is surprisedly a majority of PHP programmers. Having them optional sure, but not necessary. |
@tfont Please post feedback on the thread on internals |
This is fantastic - great work folks! :) I know this PR is still a WIP, but I was playing with it I was able to cause a segfault where I expected a parse error with this little repro: fn() ==> 42; This might already be on your radar but just wanted to report it just in case. :) Backtrace:
|
Note to self: |
@SammyK Thanks, this is fixed now. |
Can you please add a test for the case where a parameter variable also exists in the parent scope? (ensure that nothing is overwritten, because binding happens only after recv) e.g.
|
@nikic Why even have the |
@jbis9051 The syntax is discussed at length in the RFC: https://wiki.php.net/rfc/arrow_functions_v2#syntax. |
@theodorejb I should probably read the entire proposal before asking questions.....sorry |
@theodorejb I cannot find the reason of creating fn instead of reusing function, unless it is for the sake of sparing 6 extra characters. |
@jmleroy Its shorter. I would even rather |
While brevity is a factor, we also want to have some stronger syntactical distinction between closures that automatically capture scope and those that don't. This especially becomes a problem when you take into account that we may add a block variant of this syntax in the future. |
So we have a place to store whether $this and varvars are used.
Unfortunately we do have to make a conservative assumption wrt static calls.
This reverts commit 5633c47.
Independent of the reverted change.
Instead of using an empty use list to distinguish them. This also fixes the pretty printing to print an arrow function. There's still a bug here because we're missing the parentheses for the direct call, but that's an existing issue.
Also rename T_ARROW_FUNCTION to PREC_ARROW_FUNCTION, to make it obvious that this is not a real token.
Merged as f3e5bbe. |
How to write this one
arrow syntax? |
@mdalamincse Not all closures can be written as arrow functions, since they only support a single statement and don't capture variables by reference. |
Implementation for https://wiki.php.net/rfc/arrow_functions_v2.
This is based on Levi's old implementation of the same.