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:39] – bwoebi | rfc:short_closures [2015/09/01 16:30] – bwoebi | ||
---|---|---|---|
Line 3: | Line 3: | ||
* 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:// | ||
Line 28: | Line 28: | ||
</ | </ | ||
- | Anonymous functions defined in this way will automatically use() all of the compiled variables in the local scope. See the ' | + | Anonymous functions defined in this way will automatically |
==== Syntax ==== | ==== Syntax ==== | ||
Line 47: | Line 47: | ||
Omitting the parentheses when the function has multiple parameters will result in a parse error: | Omitting the parentheses when the function has multiple parameters will result in a parse error: | ||
<code php> | <code php> | ||
- | $x, $y ~> {$x + y} // Unexpected ',' | + | $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: | Using the return keyword when braces have been omitted, will similarly give a parse error: | ||
<code php> | <code php> | ||
- | ($x, $y) ~> return $x + y; // Unexpected T_RETURN | + | ($x, $y) ~> return $x + $y; // Unexpected T_RETURN |
+ | ($x, $y) ~> { return $x + $y; } // correct | ||
</ | </ | ||
- | Concrete syntax is (~> is right associative with lowest possible | + | 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 lowest possible | ||
< | < | ||
( parameter_list ) ~> expression | ( parameter_list ) ~> expression | ||
Line 68: | Line 76: | ||
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. | ||
==== Variable binding ==== | ==== 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 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 '' |
- | For example: | + | The variable binding is always **by value**. There are no implicit references. If these are needed, the current syntax with '' |
+ | For example: | ||
<code php> | <code php> | ||
$a = 1; | $a = 1; | ||
Line 98: | Line 109: | ||
==== Array sort with user function ==== | ==== Array sort with user function ==== | ||
- | Sort '' | + | Sort '' |
Current syntax: | Current syntax: | ||
Line 104: | Line 115: | ||
usort($array, | usort($array, | ||
function($a, | function($a, | ||
- | return | + | return $a->val <=> $b-> |
} | } | ||
); | ); | ||
Line 111: | Line 122: | ||
New syntax: | New syntax: | ||
<code php> | <code php> | ||
- | usort($array, | + | usort($array, |
</ | </ | ||
Line 119: | Line 130: | ||
function sumEventScores($events, | function sumEventScores($events, | ||
$types = array_map( | $types = array_map( | ||
- | function ($event) { | + | function($event) { |
return $event[' | return $event[' | ||
}, | }, | ||
Line 127: | Line 138: | ||
return array_reduce( | return array_reduce( | ||
$types, | $types, | ||
- | function ($sum, $type) use ($scores) { | + | function($sum, |
return $sum + $scores[$type]; | return $sum + $scores[$type]; | ||
} | } | ||
Line 165: | Line 176: | ||
sumEventScores($events, | sumEventScores($events, | ||
+ | </ | ||
+ | |||
+ | ==== Lazy evaluation ==== | ||
+ | It may be necessary to have code only evaluated under specific conditions, like debugging code: | ||
+ | <code php> | ||
+ | function runDebug(callable $func) { | ||
+ | /* only run under debug situations, but don't let it interrupt program flow, just log it */ | ||
+ | if (DEBUG) { | ||
+ | try { | ||
+ | $func(); | ||
+ | } catch (Exception $e) { /*... */ } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | $myFile = "/ | ||
+ | |||
+ | /* Old code */ | ||
+ | runDebug(function() use ($myFile) { /* yeah, we have to use use ($myFile) here, which isn't really helpful in this context */ | ||
+ | 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 ==== | ==== Partial application ==== | ||
- | The short hand syntax makes it easier to write functional code like a reducer by using the ability of shorthand anonymous functions to be chained together easily. | + | The shorthand |
Current syntax: | Current syntax: | ||
Line 174: | Line 216: | ||
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 213: | Line 255: | ||
return [$x => $x * 2]; | return [$x => $x * 2]; | ||
</ | </ | ||
+ | |||
+ | Additionally, | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 228: | Line 272: | ||
private $bar: | private $bar: | ||
- | getBar ~> $this-> | + | getBar() ~> $this-> |
setBar($bar) ~> $this-> | setBar($bar) ~> $this-> | ||
} | } |
rfc/short_closures.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1