rfc:arrow_functions_v2
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:arrow_functions_v2 [2019/03/13 15:46] – nikic | rfc:arrow_functions_v2 [2020/04/18 14:54] – Less party sebastian | ||
---|---|---|---|
Line 6: | Line 6: | ||
* Target version: PHP 7.4 | * Target version: PHP 7.4 | ||
* Implementation: | * Implementation: | ||
- | * Status: | + | * Status: |
===== Introduction ===== | ===== Introduction ===== | ||
Anonymous functions in PHP can be quite verbose, even when they only perform a simple operation. | Anonymous functions in PHP can be quite verbose, even when they only perform a simple operation. | ||
- | Partly this is due to a large amount of syntactic boilerplate, | + | Partly this is due to a large amount of syntactic boilerplate, |
import used variables. This makes code using simple closures hard to read and understand. This RFC | import used variables. This makes code using simple closures hard to read and understand. This RFC | ||
proposes a more concise syntax for this pattern. | proposes a more concise syntax for this pattern. | ||
Line 90: | Line 90: | ||
</ | </ | ||
- | ==== Static | + | ==== $this binding and static |
- | For the sake of completeness, static arrow functions are supported using the syntax | + | Just like normal closures, the '' |
- | < | + | |
- | bind the '' | + | |
- | Static closures are mainly | + | < |
- | which may result in delayed GC. | + | class Test { |
+ | public function method() { | ||
+ | $fn = fn() => var_dump($this); | ||
+ | $fn(); // object(Test)# | ||
+ | |||
+ | $fn = static fn() => var_dump($this); | ||
+ | $fn(); // Error: Using $this when not in object context | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Static closures are rarely used: They' | ||
+ | |||
+ | It has been suggested that we could use this opportunity to change the '' | ||
==== By-value variable binding ==== | ==== By-value variable binding ==== | ||
Line 248: | Line 259: | ||
===== Vote ===== | ===== Vote ===== | ||
- | Simple yes/no vote. | + | Voting started 2019-04-17 and ends 2019-05-01. A 2/3 majority is required. |
+ | |||
+ | <doodle title=" | ||
+ | * Yes | ||
+ | * No | ||
+ | </ | ||
===== Discussion ===== | ===== Discussion ===== | ||
Line 425: | Line 441: | ||
~($x) => $x * $y | ~($x) => $x * $y | ||
@($x) => $x * $y | @($x) => $x * $y | ||
+ | |||
+ | // Not possible, because _() is a valid function name, used as an alias for gettext() | ||
+ | _($x) => $x * $y | ||
</ | </ | ||
Line 447: | Line 466: | ||
It would be possible to resolve this ambiguity by lexing namespaced names as a single token | It would be possible to resolve this ambiguity by lexing namespaced names as a single token | ||
(removing support for whitespace inside them). This would, however, be a breaking change. | (removing support for whitespace inside them). This would, however, be a breaking change. | ||
+ | |||
+ | === Using -> and --> as arrows === | ||
+ | |||
+ | As an alternative to '' | ||
+ | |||
+ | <PHP> | ||
+ | ($x) -> $x | ||
+ | // already valid, more typically written as: | ||
+ | $x->{$x} | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | <PHP> | ||
+ | $x --> $x | ||
+ | // already valid, more typically written as: | ||
+ | $x-- > $x | ||
+ | </ | ||
+ | |||
+ | '' | ||
=== Different parameter list separators === | === Different parameter list separators === | ||
Line 516: | Line 555: | ||
This syntax is not viable in PHP, because '' | This syntax is not viable in PHP, because '' | ||
into all the same parsing issues. | into all the same parsing issues. | ||
+ | |||
+ | === Miscellaneous === | ||
+ | |||
+ | It has been suggested to use the '' | ||
+ | |||
+ | <PHP> | ||
+ | [\T &$x => $y] | ||
+ | // could be | ||
+ | [\(T &$x) => $y)] | ||
+ | // or | ||
+ | [(\T & $x) => $y] | ||
+ | </ | ||
==== Binding behavior ==== | ==== Binding behavior ==== | ||
Line 529: | Line 580: | ||
<PHP> | <PHP> | ||
$x = 1; | $x = 1; | ||
- | $fn = () => $x++; | + | $fn = fn() => $x++; |
$fn(); | $fn(); | ||
var_dump($x); | var_dump($x); | ||
Line 626: | Line 677: | ||
<PHP> | <PHP> | ||
+ | fn(params) => { | ||
+ | stmt1; | ||
+ | stmt2; | ||
+ | return expr; | ||
+ | } | ||
+ | // or possibly just | ||
fn(params) { | fn(params) { | ||
stmt1; | stmt1; | ||
Line 691: | Line 748: | ||
There are some possible variations of this, e.g. allow '' | There are some possible variations of this, e.g. allow '' | ||
+ | ===== Changelog ===== | ||
+ | |||
+ | * 2019-03-14: Clarify $this binding and explain why we're sticking with always-bind behavior. | ||
+ | * 2019-03-14: Mention '' |
rfc/arrow_functions_v2.txt · Last modified: 2020/08/01 23:53 by carusogabriel