rfc:arrow_function_preference

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
rfc:arrow_function_preference [2017/06/26 17:43]
levim old revision restored (2017/06/26 17:00)
rfc:arrow_function_preference [2017/09/22 13:28] (current)
Line 55: Line 55:
 ===== Examples ===== ===== Examples =====
  
-Here are some snippets from real code-bases converted to use both syntaxes:+Here are some snippets from real code-bases converted to use the syntaxes:
  
 <PHP> <PHP>
 $s1 = fn($c) => $callable($factory($c),​ $c); $s1 = fn($c) => $callable($factory($c),​ $c);
 $s2 = {$c => $callable($factory($c),​ $c)}; $s2 = {$c => $callable($factory($c),​ $c)};
 +</​PHP>​ 
 +<PHP>
 $this->​existingSchemaPaths = array_filter($paths,​ fn($v) => in_array($v,​ $names)); $this->​existingSchemaPaths = array_filter($paths,​ fn($v) => in_array($v,​ $names));
 $this->​existingSchemaPaths = array_filter($paths,​ {$v => in_array($v,​ $names)}); $this->​existingSchemaPaths = array_filter($paths,​ {$v => in_array($v,​ $names)});
 +</​PHP>​ 
 +<PHP>
 function complement_s1(callable $f) { function complement_s1(callable $f) {
-    return fn(... $args) => !$f(... $args);+    return fn(...$args) => !$f(...$args);​
 } }
 function complement_s2(callable $f) { function complement_s2(callable $f) {
-    return {(... $args) => !$f(... $args)};+    return {(...$args) => !$f(...$args)};​
 } }
 +</​PHP>​ 
 +<PHP>
 function reject_s1($collection,​ callable $function) { function reject_s1($collection,​ callable $function) {
     return filter($collection,​ fn($value, $key) => !$function($value,​ $key));     return filter($collection,​ fn($value, $key) => !$function($value,​ $key));
Line 77: Line 80:
     return filter($collection,​ {($value, $key) => !$function($value,​ $key)});     return filter($collection,​ {($value, $key) => !$function($value,​ $key)});
 } }
 +</​PHP>​ 
 +<PHP>
 // not real code but typical of such // not real code but typical of such
 $result = Collection::​from([1,​ 2]) $result = Collection::​from([1,​ 2])
Line 89: Line 93:
 I'll point out only one thing and leave the rest to the you to analyze: the closing curly brace ''​}''​ makes it a bit easier to distinguish the end of the expression when the closure is directly passed as a parameter. I'll point out only one thing and leave the rest to the you to analyze: the closing curly brace ''​}''​ makes it a bit easier to distinguish the end of the expression when the closure is directly passed as a parameter.
  
-As an example of all the places references can be used (needs a mono-space font for the arrows to align):+=== Of references === 
 +As an example of all the places references can be used:
  
     &fn &​(&​$x) => function_that_takes_ref_and_returns_ref($x,​ $y)     &fn &​(&​$x) => function_that_takes_ref_and_returns_ref($x,​ $y)
Line 104: Line 109:
     |  \ Function returns by reference     |  \ Function returns by reference
     \ Variables bound from the outer-scope are bound by reference     \ Variables bound from the outer-scope are bound by reference
 +
 +[[https://​chat.stackoverflow.com/​users/​4251625/​wes-stark|Wes Stark]] suggests that we move the return-by-reference sigil to behind the parameters where we put return type information:​
 +
 +    {($x): & => function_that_returns_ref($x,​ $y) }
 +           ^
 +           \ Function returns by reference
 +
 +This cleans up the potential density if you close by reference and return by reference but means that we have a break in convention.
 +
 +=== Of Nested Closures ===
 +
 +Nikita Popov requested that I show an example of nested closures:
 +
 +<PHP>
 +$add2 = fn($x) => fn($y) => $x + $y;
 +
 +$add2 = {$x => {$y => $x + $y}};
 +</​PHP>​
  
 ===== Vote ===== ===== Vote =====
  
-<doodle title="​Preference of Arrow Function Syntax for PHP 7.2" auth="​levim voteType="​single" closed="​true">​+<doodle title="​Preference of Arrow Function Syntax for PHP 7.2" auth="​levim voteType="​multi" closed="​true">​ 
 +   * Prefixed with fn 
 +   * Enclosed by curly-braces 
 +   * Reuse function 
 +   * No Preference / Other Preference 
 +</​doodle>​ 
 + 
 + 
 +<doodle title="​Practice vote for Preference of Arrow Function Syntax for PHP 7.2" auth="​levim voteType="​multi"​ closed="​TRUE">
    * Prefixed with fn    * Prefixed with fn
    * Enclosed by curly-braces    * Enclosed by curly-braces
-   * No Preference+   * Reuse function 
 +   * No Preference / Other Preference
 </​doodle>​ </​doodle>​
  
rfc/arrow_function_preference.1498498980.txt.gz · Last modified: 2017/09/22 13:28 (external edit)