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/07/05 06:02]
levim Add 3rd option?
rfc:arrow_function_preference [2017/07/06 16:55]
levim
Line 52: Line 52:
  
 This syntax would restrict the ability to add object literals a-la JavaScript. Specifically it would prevent the ability to use arbitrary expressions as object literal keys. This syntax would restrict the ability to add object literals a-la JavaScript. Specifically it would prevent the ability to use arbitrary expressions as object literal keys.
- 
-==== Option 3 ==== 
- 
-This option is the same as option 1 except that instead of adding a new keyword ''​T_FN''​ we just re-use ''​T_FUNCTION'':​ 
- 
-    T_FUNCTION '​('​ parameter_list '​)'​ T_DOUBLE_ARROW expr 
  
 ===== Examples ===== ===== Examples =====
  
-Here are some snippets from real code-bases converted to use the three 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)};
-$s3 = function ($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)});
-$this->existingSchemaPaths = array_filter($paths,​ function($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);​
Line 78: Line 72:
     return {(...$args) => !$f(...$args)};​     return {(...$args) => !$f(...$args)};​
 } }
-function complement_s3(callable $f) { +</​PHP>​ 
-    ​return function(...$args) =!$f(...$args);​ +<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 88: Line 80:
     return filter($collection,​ {($value, $key) => !$function($value,​ $key)});     return filter($collection,​ {($value, $key) => !$function($value,​ $key)});
 } }
-function reject_s3($collection,​ callable $function) { +</​PHP>​ 
-    ​return filter($collection,​ function($value,​ $key) =!$function($value,​ $key)); +<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 99: Line 89:
     ->​map({$v => $v * 2})     ->​map({$v => $v * 2})
     ->​reduce({($tmp,​ $v) => $tmp + $v}, 0);     ->​reduce({($tmp,​ $v) => $tmp + $v}, 0);
-$result = Collection::​from([1,​ 2]) 
-    ->​map(function($v) => $v * 2) 
-    ->​reduce(function($tmp,​ $v) => $tmp + $v, 0); 
 </​PHP>​ </​PHP>​
  
Line 121: Line 108:
     |  | \ Parameter is passed by reference     |  | \ Parameter is passed by reference
     |  \ Function returns by reference     |  \ Function returns by reference
-    \ Variables bound from the outer-scope are bound by reference 
-    ​ 
-And in the last option: 
- 
-    &​function &​(&​$x) => function_that_takes_ref_and_returns_ref($x,​ $y) } 
-    ^         ^ ^ 
-    |         | \ Parameter is passed 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
  
Line 147: Line 126:
  
 $add2 = {$x => {$y => $x + $y}}; $add2 = {$x => {$y => $x + $y}};
- 
-$add2 = function($x) => function($y) => $x + $y; 
 </​PHP>​ </​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
    * Reuse function    * Reuse function
-   * No Preference+   * No Preference / Other Preference
 </​doodle>​ </​doodle>​
  
rfc/arrow_function_preference.txt · Last modified: 2017/09/22 13:28 (external edit)