rfc:short_closures
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:short_closures [2015/05/01 18:18] – old revision restored (2015/05/01 04:20) bwoebi | rfc:short_closures [2015/09/22 01:31] – bwoebi | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Short Closures ====== | ====== PHP RFC: Short Closures ====== | ||
- | * Version: 0.1 | + | * Version: 0.2 |
* Date: 2015-05-01 | * Date: 2015-05-01 | ||
* Author: Bob Weinand, bobwei9@hotmail.com | * Author: Bob Weinand, bobwei9@hotmail.com | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | When writing partial | + | Anonymous |
+ | |||
+ | The current implementation of anonymous functions in PHP is quite verbose compared | ||
+ | |||
+ | A better syntax encourages functional code and partial applications | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes the introduction of the ~> operator. | + | This RFC proposes the introduction of the ~> operator |
- | The ~> operator defines a shorthand Closure which automatically use()'s all the used compiled variables | + | Current code: |
+ | <code php> | ||
+ | function ($x) { | ||
+ | return $x * 2; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | would be equivalent to the new syntax: | ||
+ | <code php> | ||
+ | $x ~> $x * 2 | ||
+ | </ | ||
+ | |||
+ | Anonymous functions defined in this way will automatically | ||
+ | |||
+ | ==== Syntax ==== | ||
+ | The syntax used to define a short hand anonymous function would be: | ||
+ | |||
+ | * Parameters. When the function has a single parameter the surrounding parentheses (aka round brackets) may be omitted. For functions with multiple parameters the parentheses are required. | ||
+ | * The new short closure operator ~> | ||
+ | * The body of the anonymous function. When the body of the function is a single expression the surrounding curly brackets and return keyword may be omitted. When the body of the function is not a single expression, the braces | ||
+ | |||
+ | I.e. all of the following would be equivalent: | ||
+ | <code php> | ||
+ | $x ~> $x * 2 | ||
+ | $x ~> { return $x * 2;} | ||
+ | ($x) ~> $x * 2 | ||
+ | ($x) ~> { return $x * 2; } | ||
+ | </ | ||
+ | |||
+ | Omitting the parentheses when the function has multiple parameters will result in a parse error: | ||
+ | <code php> | ||
+ | $x, $y ~> {$x + $y} // Unexpected ',' | ||
+ | ($x, $y) ~> $x + $y // correct | ||
+ | </ | ||
+ | |||
+ | Using the return keyword when braces have been omitted, will similarly give a parse error: | ||
+ | <code php> | ||
+ | ($x, $y) ~> return $x + $y; // Unexpected T_RETURN | ||
+ | ($x, $y) ~> { return $x + $y; } // correct | ||
+ | </ | ||
+ | |||
+ | In case of no parameters, an empty parenthesis pair is needed. | ||
+ | <code php> | ||
+ | ~> 2 * 3; // Unexpected T_TILDED_ARROW | ||
+ | () ~> 2 * 3; // correct, will return 6 when called | ||
+ | </ | ||
- | Concrete syntax is (~> is right associative with highest | + | Concrete syntax is (~> is right associative with lowest |
< | < | ||
( parameter_list ) ~> expression | ( parameter_list ) ~> expression | ||
Line 25: | Line 74: | ||
| $variable ~> { statements } | | $variable ~> { statements } | ||
</ | </ | ||
+ | |||
When a bare expression is used as second parameter, its result will be the return value of the Closure. | When a bare expression is used as second parameter, its result will be the return value of the Closure. | ||
+ | |||
+ | Also, parameter_list does //not// include default values nor type hints. See also the 'Type Hints and Return Types' section at the bottom. | ||
+ | |||
+ | // | ||
+ | This RFC stance is that chained short Closures followed by a full Closure would look quite weird: '' | ||
+ | |||
+ | // | ||
+ | While it might appear not consistent, with any other number of parameters, a lot of languages having extra short Closures allow this. Also, Closures with just one parameter are relatively common, so this RFC author thinks it is worth supporting that. | ||
+ | |||
+ | ==== Variable binding ==== | ||
+ | The position of this RFC is that the shorthand syntax is to allow anonymous functions to be used as easily as possible. Therefore, rather than requiring individual variables be bound to the closure through the '' | ||
+ | |||
+ | The variable binding is always **by value**. There are no implicit references. If these are needed, the current syntax with '' | ||
+ | |||
+ | For example: | ||
+ | <code php> | ||
+ | $a = 1; | ||
+ | function foo(array $input, $b) { | ||
+ | $c = rand(0, 4); | ||
+ | |||
+ | return array_map($x ~> ($x * 2) + $b + $c, $input); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Variables $b and $c would be bound automatically to the anonymous function, and so be usable inside it. Variable $a is not in the scope of the function, and so is not bound, and so cannot be used inside the closure. e.g. this code will give an error: | ||
+ | <code php> | ||
+ | $a = 1; | ||
+ | function foo(array $input, $b) { | ||
+ | // Notice: Undefined variable: a in %s on line %d | ||
+ | return array_map($x ~> ($x * 2) + $b + $a, $input); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | If a user wants to avoid binding all variables automatically they can use the current syntax to define the anonymous function. | ||
===== Examples ===== | ===== Examples ===== | ||
- | ==== Simple | + | These examples cover some simple operations and show how the short-hand syntax is easier to read compared to the existing long-hand syntax. |
+ | |||
+ | ==== Array sort with user function | ||
+ | Sort '' | ||
+ | |||
+ | Current syntax: | ||
<code php> | <code php> | ||
- | $number = 5; | + | usort($array, |
- | $func = $a ~> $number * $a; /* $number is implicitly imported into the Closure */ | + | function($a, $b) { |
- | var_dump($func(10)); // int 15 | + | return |
+ | } | ||
+ | ); | ||
+ | </code> | ||
- | /* Compared to */ | + | New syntax: |
- | $func = function($a) use ($number) { | + | <code php> |
- | return | + | usort($array, |
- | }; | + | |
</ | </ | ||
- | ==== Quick applications to arrays | + | ==== Extracting data from an array and summing it ==== |
+ | Current syntax: | ||
<code php> | <code php> | ||
- | /* Get a range from 2 to 10 with increment 2 */ | + | function sumEventScores($events, |
- | $array = array_map($x ~> $x * 2, range(1, 5)); | + | $types = array_map( |
+ | function($event) { | ||
+ | return | ||
+ | }, | ||
+ | $events | ||
+ | | ||
- | /* Compared to */ | + | return array_reduce( |
- | foreach | + | $types, |
- | $array[] = $x * 2; | + | |
+ | | ||
+ | } | ||
+ | ); | ||
} | } | ||
- | /* or */ | ||
- | $array = array_map(function($x) { return $x * 2; }, range(1, 5)); | ||
</ | </ | ||
+ | New syntax: | ||
<code php> | <code php> | ||
- | /* Let $array be an array filled with objects having property val. Sort them in reverse by that property. */ | + | function sumEventScores($events, $scores) { |
- | usort($array, ($a, $b) ~> -($a->val < | + | |
+ | return array_reduce($types, ($sum, $type) ~> $sum + $scores[$type]); | ||
+ | } | ||
+ | </ | ||
- | /* Compared to */ | + | The calling code for this function would be: |
- | usort($array, function($a, $b) { return | + | |
- | /* ... which will be probably multilined in a lot of places... */ | + | <code php> |
- | usort($array, function($a, $b) { | + | $events = array( |
- | | + | array( |
- | }); | + | ' |
+ | ' | ||
+ | ), | ||
+ | array( | ||
+ | ' | ||
+ | ' | ||
+ | | ||
+ | //... | ||
+ | ); | ||
+ | |||
+ | $scores = [ | ||
+ | ' | ||
+ | ' | ||
+ | | ||
+ | ' | ||
+ | ]; | ||
+ | |||
+ | sumEventScores($events, | ||
</ | </ | ||
- | ==== Partial application | + | ==== Lazy evaluation |
+ | It may be necessary to have code only evaluated under specific conditions, like debugging code: | ||
<code php> | <code php> | ||
- | function | + | function |
- | | + | |
+ | if (DEBUG) { | ||
+ | try { | ||
+ | | ||
+ | } catch (Exception $e) { /*... */ } | ||
+ | } | ||
} | } | ||
- | /* Just to compare to the old equivalent | + | $myFile = "/ |
- | function | + | |
- | return function($left) { | + | /* Old code */ |
- | return function($right) use ($left) { | + | runDebug(function() |
- | | + | if (!file_exists($myFile)) { |
- | } | + | throw new Exception(" |
} | } | ||
- | } | + | }); |
+ | |||
+ | /* New code */ | ||
+ | runDebug(() ~> { | ||
+ | if (!file_exists($myFile)) { | ||
+ | throw new Exception(" | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | /* still continue here, unlike an assert which would unwind the stack frame here ... */ | ||
</ | </ | ||
+ | |||
+ | ==== Partial application ==== | ||
+ | The shorthand syntax makes it easier to write functional code like a reducer by using the ability of shorthand anonymous functions to be chained together easily. | ||
+ | |||
+ | Current syntax: | ||
<code php> | <code php> | ||
- | /* Thanks to Levi Morrison for that example */ | ||
- | function reduce(callable $fn) { | ||
- | return $initial ~> $input ~> { | ||
- | $accumulator = $initial; | ||
- | foreach ($input as $value) { | ||
- | $accumulator = $fn($accumulator, | ||
- | } | ||
- | return $accumulator; | ||
- | }; | ||
- | } | ||
- | |||
- | /* Compared to */ | ||
function reduce(callable $fn) { | function reduce(callable $fn) { | ||
return function($initial) use ($fn) { | return function($initial) use ($fn) { | ||
- | return function ($input) use ($fn, $initial) { | + | return function($input) use ($fn, $initial) { |
$accumulator = $initial; | $accumulator = $initial; | ||
foreach ($input as $value) { | foreach ($input as $value) { | ||
Line 106: | Line 233: | ||
</ | </ | ||
- | ===== General thoughts ===== | + | New syntax: |
- | ==== Do we really need this? ==== | + | <code php> |
- | Ultimately, it does not add any additional possibilities which weren' | + | function reduce(callable $fn) { |
+ | return $initial ~> $input ~> { | ||
+ | $accumulator | ||
+ | | ||
+ | $accumulator | ||
+ | } | ||
+ | | ||
+ | }; | ||
+ | } | ||
+ | </ | ||
- | But it prevents us from having to write too much boilerplate, | + | ===== Symbol choice ===== |
- | Aside from that, it encourages (or makes it at least much tedious task) functional code and partial applications (see the examples), which may be a powerful tool the language shouldn' | + | The symbol '' |
- | ==== Why ~> ? ==== | + | Currently Hack has implemented shorthand anonymous functions using the '' |
- | Hack has %%==> | + | |
- | ~> has the advantage of being shorter and it doesn' | + | This returns an array containing an anonymous function: |
+ | <code php> | ||
+ | return [$x ==> $x * 2]; | ||
+ | </code> | ||
- | That's why this RFC proposes | + | This returns an array if $x is already a defined variable. |
+ | <code php> | ||
+ | return [$x => $x * 2]; | ||
+ | </ | ||
+ | |||
+ | Additionally, | ||
+ | |||
+ | Also, Hack has some possibilities of typing here, which do not work with PHP, due to technical reasons. Regarding forward compatibility, | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 128: | Line 273: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | This RFC _does not_ propose | + | ==== Other uses for ~> operator ==== |
+ | This RFC is solely for using the shorthand | ||
<code php> | <code php> | ||
class Foo { | class Foo { | ||
private $bar: | private $bar: | ||
- | getBar ~> $this-> | + | getBar() ~> $this-> |
setBar($bar) ~> $this-> | setBar($bar) ~> $this-> | ||
} | } | ||
</ | </ | ||
- | ===== Proposed Voting Choices | + | Which is outside the scope of this RFC. |
+ | |||
+ | ==== Type Hints and Return Types ==== | ||
+ | This RFC does //not// include type hints nor return types. | ||
+ | |||
+ | Type Hints are not added due to technical problems in parser and the RFC author is not sure about whether they should be really added. If anyone achieves to solve these technical issues, he should feel free to do that in a future RFC for further discussion. | ||
+ | And as introducing half a typesystem would be inconsistent, | ||
+ | |||
+ | As an alternative, | ||
+ | |||
+ | ===== Vote ===== | ||
This RFC is a language change and as such needs a 2/3 majority. | This RFC is a language change and as such needs a 2/3 majority. | ||
- | It will be a simple yes/no vote. | + | Voting opened September 22th, 2015 and will remain open until October 2nd, 2015. |
- | ===== Patches and Tests ===== | + | <doodle title="Short Closures" |
- | Pull request is at https:// | + | * Yes |
+ | * No | ||
+ | </doodle> | ||
- | ===== | + | ===== Patch ===== |
+ | Pull request is at https:// |
rfc/short_closures.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1