rfc:arrow_functions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:arrow_functions [2017/01/17 04:29] – Add spaces levim | rfc:arrow_functions [2017/01/31 17:27] – Fix link formatting levim | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Arrow Functions ====== | ====== PHP RFC: Arrow Functions ====== | ||
- | * Version: 1.2 | + | * Version: 1.3 |
* Date: 2016-08-14 | * Date: 2016-08-14 | ||
* Author: Levi Morrison < | * Author: Levi Morrison < | ||
* Author: Bob Weinand < | * Author: Bob Weinand < | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 10: | Line 10: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Anonymous functions and closures can be verbose even though sometimes they are quite simple and contain only a single expression. Additionally, | + | Anonymous functions and closures can be verbose even though sometimes they are quite simple and contain only a single expression. Additionally, |
As an example of the declaration overhead, consider this function that [[https:// | As an example of the declaration overhead, consider this function that [[https:// | ||
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. | ||
+ | |||
+ | ==== References ==== | ||
+ | Parameters and return values can be passed/ | ||
+ | |||
+ | ==== 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 '' |
- | ==== Patches and Tests ==== | + | Ilija Tovilo analyzed the top 1,000 PHP repositories on GitHub to find usages of '' |
- | An old implementation with tests can be found here: https:// | + | |
- | It will need to be heavily rebased to fit on master. | + | ==== Patches and Tests ==== |
- | + | An implementation with tests can be found here: https:// | |
- | ==== PHP Version | + | |
- | This RFC targets PHP 7.NEXT, currently version 7.2. | + | |
==== Voting ==== | ==== Voting ==== | ||
Line 90: | Line 92: | ||
----- | ----- | ||
- | ==== Examples ==== | + | ===== Examples ===== |
- | + | ||
- | === Snippets | + | |
Taken from [[https:// | Taken from [[https:// | ||
Line 100: | Line 100: | ||
// 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 113: | Line 113: | ||
// 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 129: | Line 129: | ||
// 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 148: | Line 148: | ||
// 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