rfc:arrow_functions

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:arrow_functions [2017/01/29 16:37] – Add Ilija's analysis of BC breaks. levimrfc:arrow_functions [2017/01/31 18:24] – Show examples of types and references levim
Line 1: Line 1:
-ach ====== PHP RFC: Arrow Functions ======+====== PHP RFC: Arrow Functions ======
   * Version: 1.3   * 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 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 ====
 +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.
  
-===== Examples =====+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.
  
-==== Snippets ====+==== Patches and Tests ==== 
 +An implementation with tests can be found here: https://github.com/morrisonlevi/php-src/tree/arrow_functions. There are no known issues with it at this time; please build and test it. 
 + 
 +==== Voting ==== 
 +Voting will be a simple Yes/No that requires 2/3 or more of the votes to be "Yes" to be accepted. 
 + 
 +----- 
 + 
 +<doodle title="Accept arrow functions? (2/3 required)" auth="levijm" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle> 
 + 
 +----- 
 + 
 +===== Examples =====
 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 109: Line 143:
 }</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])
Line 131: Line 165:
 </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:
  
Line 147: Line 179:
  
 This RFC omitted this feature for these reasons. If arrow functions are accepted and become more common it may make sense to revisit this feature. This RFC omitted this feature for these reasons. If arrow functions are accepted and become more common it may make sense to revisit this feature.
- 
-===== 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 ''<nowiki>=></nowiki>'' for array and yield keys. 
- 
-Ilija Tovilo analyzed the top 1,000 PHP repositories on GitHub to find usages of ''fn''. [The gist](https://gist.github.com/morrisonlevi/473a7e0cb6e59c830224b1c71b8da28c) 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. 
- 
-===== Patches and Tests ===== 
-An implementation with tests can be found here: https://github.com/morrisonlevi/php-src/tree/arrow_functions. There are no known issues with it at this time; please build and test it. 
- 
-===== Voting ===== 
-Voting will be a simple Yes/No that requires 2/3 or more of the votes to be "Yes" to be accepted. 
- 
------ 
- 
-<doodle title="Accept arrow functions? (2/3 required)" auth="levijm" voteType="single" closed="true"> 
-   * Yes 
-   * No 
-</doodle> 
  
rfc/arrow_functions.txt · Last modified: 2018/06/28 14:35 by levim