rfc:arrow_functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
rfc:arrow_functions [2017/01/17 04:28]
levim Re-introduce `fn`
rfc:arrow_functions [2017/01/31 18:24]
levim Show examples of types and references
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 <levim@php.net>   * Author: Levi Morrison <levim@php.net>
   * Author: Bob Weinand <bwoebi@php.net>   * Author: Bob Weinand <bwoebi@php.net>
-  * Status: Draft+  * Status: Under Discussion
   * First Published at: http://wiki.php.net/rfc/arrow_functions   * First Published at: http://wiki.php.net/rfc/arrow_functions
  
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, importing variables into the closure's scope is manual and is painful overhead for single-expression closures. This RFC proposes a shorter syntax for anonymous functions and closures and makes it easier for these new closures to capture values/variables.+Anonymous functions and closures can be verbose even though sometimes they are quite simple and contain only a single expression. Additionally, importing variables into the closure's scope is manual and is painful overhead for single-expression closures. In practice these single-expression closures are common. This RFC proposes a more concise syntax for this pattern.
  
 As an example of the declaration overhead, consider this function that [[https://github.com/darkskillfr/near2u/blob/5a606fc9082c33c7270d37e4c7d29160586285f8/serveur/lib.php|I found online]]: As an example of the declaration overhead, consider this function that [[https://github.com/darkskillfr/near2u/blob/5a606fc9082c33c7270d37e4c7d29160586285f8/serveur/lib.php|I found online]]:
  
 <PHP>function array_values_from_keys($arr, $keys) { <PHP>function array_values_from_keys($arr, $keys) {
-    return array_map(function($x) use ($arr) { return $arr[$x]; }, $keys);+    return array_map(function ($x) use ($arr) { return $arr[$x]; }, $keys);
 }</PHP> }</PHP>
  
Line 21: Line 21:
  
 <PHP>function array_values_from_keys($arr, $keys) { <PHP>function array_values_from_keys($arr, $keys) {
-    return array_map(fn ($x) => $arr[$x], $keys);+    return array_map(fn($x) => $arr[$x], $keys);
 }</PHP> }</PHP>
  
Line 34: Line 34:
  
 <PHP> <PHP>
-fn (parameter_list) => expr+fn(parameter_list) => expr
 </PHP> </PHP>
  
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) {
     return $x + $y;     return $x + $y;
 };</PHP> };</PHP>
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
 +</PHP>
 +
 +==== References ====
 +Parameters and return values can be passed/returned by reference. As mentioned elsewhere, implicitly bound variables will be bound //by value// and not //by reference//. References go in the usual places:
 +
 +<PHP>
 +fn &(array &$xs) => $xs
 +</PHP>
 +
 +==== Static Arrow Functions ====
 +The implementation currently supports static closures, for example <php>static fn($x) => static::get($x)</php>. While supported it is uncertain whether it should be included in the final version. Having the implementation support it allows testers to determine usefulness and value.
  
 ==== Ambiguities ==== ==== Ambiguities ====
 Arrow functions have no ambiguities, including array key definitions and yield expressions that provide a key. The ''fn'' prefix removes the ambiguities. Arrow functions have no ambiguities, including array key definitions and yield expressions that provide a key. The ''fn'' prefix removes the ambiguities.
- 
 ==== Backward Incompatible Changes ==== ==== Backward Incompatible Changes ====
-Unfortunately the ''fn'' keyword must be a full keyword and not just a reserved function name; this is to break the ambiguities with ''=>'' for array and yield keys.+Unfortunately the ''fn'' keyword must be a full keyword and not just a reserved function name; this is to break the ambiguities with ''<nowiki>=></nowiki>'' for array and yield keys.
  
-==== Patches and Tests ==== +Ilija Tovilo analyzed the top 1,000 PHP repositories on GitHub to find usages of ''fn''. [[https://gist.github.com/morrisonlevi/473a7e0cb6e59c830224b1c71b8da28c|The gist]] provides more information, but the rough findings are that all known existing usages of ''fn'' are in tests except one case where it is a namespace segment.
-An old implementation with tests can be found here: https://github.com/morrisonlevi/php-src/tree/arrow_functions. This patch was feature-complete for an old version of this RFC that used ''^'' to prefix the function expressions. The implementation can still be used but portion that deals with the grammar must be rewritten.+
  
-It will need to be heavily rebased to fit on master. +==== Patches and Tests ==== 
- +An implementation with tests can be found here: https://github.com/morrisonlevi/php-src/tree/arrow_functionsThere are no known issues with it at this time; please build and test it.
-==== PHP Version ==== +
-This RFC targets PHP 7.NEXT, currently version 7.2.+
  
 ==== Voting ==== ==== Voting ====
Line 90: Line 103:
 ----- -----
  
-==== Examples ==== +===== Examples =====
- +
-=== Snippets ===+
 Taken from [[https://github.com/silexphp/Pimple/blob/62b5d317a83b02eea42b5b785b62a29fba458bcf/src/Pimple/Container.php#L242-L244|silexphp/Pimple]]: Taken from [[https://github.com/silexphp/Pimple/blob/62b5d317a83b02eea42b5b785b62a29fba458bcf/src/Pimple/Container.php#L242-L244|silexphp/Pimple]]:
  
Line 100: Line 111:
  
 // with arrow function: // with arrow function:
-$extended = fn ($c) => $callable($factory($c), $c);</PHP>+$extended = fn($c) => $callable($factory($c), $c);</PHP>
  
 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 124:
  
 // with arrow function // with arrow function
-$this->existingSchemaPaths = array_filter($paths, fn ($v) => in_array($v, $names));</PHP>+$this->existingSchemaPaths = array_filter($paths, fn($v) => in_array($v, $names));</PHP>
  
 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 122: Line 133:
  
 <PHP>function complement(callable $f) { <PHP>function complement(callable $f) {
-    return function(... $args) use ($f) {+    return function (... $args) use ($f) {
         return !$f(... $args);         return !$f(... $args);
     };     };
Line 129: 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);
 }</PHP> }</PHP>
  
-=== Longer Examples ===+-----
  
-The following examples were given to me by [[https://gist.github.com/tpunt/b4f9bf30f43b9e148b73ce18245ab472|tpunt]]:+The following example was given to me by [[https://gist.github.com/tpunt/b4f9bf30f43b9e148b73ce18245ab472|tpunt]]:
  
 <PHP>$result = Collection::from([1, 2]) <PHP>$result = Collection::from([1, 2])
-    ->map(function($v) {+    ->map(function ($v) {
         return $v * 2;         return $v * 2;
     })     })
-    ->reduce(function($tmp, $v) {+    ->reduce(function ($tmp, $v) {
         return $tmp + $v;         return $tmp + $v;
     }, 0);     }, 0);
Line 148: Line 159:
 // with arrow functions: // with arrow functions:
 $result = Collection::from([1, 2]) $result = Collection::from([1, 2])
-    ->map(fn ($v) => $v * 2) +    ->map(fn($v) => $v * 2) 
-    ->reduce(fn ($tmp, $v) => $tmp + $v, 0);+    ->reduce(fn($tmp, $v) => $tmp + $v, 0);
  
 echo $result; //6 echo $result; //6
 </PHP> </PHP>
  
-===== Future Scope ===== +===== Future ScopeMulti-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