rfc:arrow_functions
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 [2017/01/21 19:33] – Version 1.3 levim | rfc:arrow_functions [2017/09/22 13:28] – external edit 127.0.0.1 | ||
---|---|---|---|
Line 4: | Line 4: | ||
* Author: Levi Morrison < | * Author: Levi Morrison < | ||
* Author: Bob Weinand < | * Author: Bob Weinand < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 21: | Line 21: | ||
< | < | ||
- | return array_map(fn ($x) => $arr[$x], $keys); | + | return array_map(fn($x) => $arr[$x], $keys); |
}</ | }</ | ||
Line 34: | Line 34: | ||
<PHP> | <PHP> | ||
- | fn (parameter_list) => expr | + | fn(parameter_list) => expr |
</ | </ | ||
Line 40: | Line 40: | ||
<PHP> | <PHP> | ||
- | $mul2 = fn ($x) => $x * 2; | + | $mul2 = fn($x) => $x * 2; |
$mul2(3); // evaluates to 6 | $mul2(3); // evaluates to 6 | ||
Line 51: | Line 51: | ||
$y = 1; | $y = 1; | ||
- | $versionA = fn ($x) => $x + $y; | + | $versionA = fn($x) => $x + $y; |
$versionB = function ($x) use ($y) { | $versionB = function ($x) use ($y) { | ||
Line 63: | Line 63: | ||
==== Type Declarations ==== | ==== Type Declarations ==== | ||
This RFC does support type declarations for parameters and return types. This issue was noted multiple times on the mailing list during the short closures RFC as something that bothered voters. Therefore this RFC permits them but the authors discourage their general use in arrow functions. | This RFC does support type declarations for parameters and return types. This issue was noted multiple times on the mailing list during the short closures RFC as something that bothered voters. Therefore this RFC permits them but the authors discourage their general use in arrow functions. | ||
+ | |||
+ | Here are some examples to show the syntax: | ||
+ | |||
+ | <PHP> | ||
+ | fn (array $x) => $x | ||
+ | fn (): int => 42 | ||
+ | </ | ||
+ | |||
+ | ==== References ==== | ||
+ | Parameters and return values can be passed/ | ||
+ | |||
+ | <PHP> | ||
+ | fn &(array &$xs) => $xs | ||
+ | </ | ||
+ | |||
+ | ==== Static Arrow Functions ==== | ||
+ | The implementation currently supports static closures, for example < | ||
==== Ambiguities ==== | ==== Ambiguities ==== | ||
Arrow functions have no ambiguities, | Arrow functions have no ambiguities, | ||
- | |||
==== Backward Incompatible Changes ==== | ==== Backward Incompatible Changes ==== | ||
- | Unfortunately the '' | + | Unfortunately the '' |
+ | |||
+ | Ilija Tovilo analyzed the top 1,000 PHP repositories on GitHub to find usages of '' | ||
==== Patches and Tests ==== | ==== Patches and Tests ==== | ||
Line 85: | Line 103: | ||
----- | ----- | ||
- | ==== Examples ==== | + | ===== Examples ===== |
- | + | ||
- | === Snippets | + | |
Taken from [[https:// | Taken from [[https:// | ||
Line 95: | Line 111: | ||
// with arrow function: | // with arrow function: | ||
- | $extended = fn ($c) => $callable($factory($c), | + | $extended = fn($c) => $callable($factory($c), |
This reduces the amount of boilerplate from 44 characters down to 8. | This reduces the amount of boilerplate from 44 characters down to 8. | ||
Line 108: | Line 124: | ||
// with arrow function | // with arrow function | ||
- | $this-> | + | $this-> |
This reduces the amount of boilerplate from 31 characters down to 8. | This reduces the amount of boilerplate from 31 characters down to 8. | ||
Line 124: | Line 140: | ||
// with arrow function: | // with arrow function: | ||
function complement(callable $f) { | function complement(callable $f) { | ||
- | return fn (... $args) => !$f(... $args); | + | return fn(... $args) => !$f(... $args); |
}</ | }</ | ||
- | === Longer Examples === | + | ----- |
- | The following | + | The following |
< | < | ||
Line 143: | Line 159: | ||
// with arrow functions: | // with arrow functions: | ||
$result = Collection:: | $result = Collection:: | ||
- | ->map(fn ($v) => $v * 2) | + | -> |
- | -> | + | -> |
echo $result; //6 | echo $result; //6 | ||
</ | </ | ||
- | ===== Future Scope ===== | + | ===== Future Scope: Multi-Statement Bodies |
- | + | ||
- | ==== Multi-Statement Bodies ==== | + | |
Some languages permit multi-statement closures with a syntax like: | Some languages permit multi-statement closures with a syntax like: | ||
rfc/arrow_functions.txt · Last modified: 2018/06/28 14:35 by levim