rfc:operator_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:operator_functions [2017/09/08 23:51] – v1.0.2 mention omissions ajfrfc:operator_functions [2017/09/09 00:26] – clarify UVS ajf
Line 78: Line 78:
 The table below lists the new functions that would be added to the root namespace (''\''). Each is named the same as its corresponding operator, including any aliases (for the sake of consistency). The table below lists the new functions that would be added to the root namespace (''\''). Each is named the same as its corresponding operator, including any aliases (for the sake of consistency).
  
-^ Function signature        ^ Corresponding operation             ^ +^ Function signature        ^ Corresponding operation             Notes                       ^ 
-^ <php>'+'($a[, $b])</php>  <php>+$a</php>, <php>$a + $b</php>  ^ +<php>'+'($a[, $b])</php>  <php>+$a</php>, <php>$a + $b</php>  |                             | 
-<php>'-'($a[, $b])</php>  <php>-$a</php>, <php>$a - $b</php>  ^ +<php>'-'($a[, $b])</php>  <php>-$a</php>, <php>$a - $b</php>  |                             | 
-<php>'*'($a, $b)</php>    <php>$a * $b</php>                  ^ +<php>'*'($a, $b)</php>    <php>$a * $b</php>                  |                             | 
-<php>'/'($a, $b)</php>    <php>$a / $b</php>                  ^ +<php>'/'($a, $b)</php>    <php>$a / $b</php>                  |                             | 
-<php>'%'($a, $b)</php>    <php>$a % $b</php>                  ^ +<php>'%'($a, $b)</php>    <php>$a % $b</php>                  |                             | 
-<php>'**'($a, $b)</php>   <php>$a ** $b</php>                 ^ +<php>'**'($a, $b)</php>   <php>$a ** $b</php>                 |                             | 
-<php>'&'($a, $b)</php>    <php>$a & $b</php>                  ^ +<php>'&'($a, $b)</php>    <php>$a & $b</php>                  |                             | 
-<php>'|'($a, $b)</php>    <php>$a | $b</php>                  ^ +<php>'|'($a, $b)</php>    <php>$a | $b</php>                  |                             | 
-<php>'^'($a, $b)</php>    <php>$a ^ $b</php>                  ^ +<php>'^'($a, $b)</php>    <php>$a ^ $b</php>                  |                             | 
-<php>'~'($a)</php>        <php>~$a</php>                      ^ +<php>'~'($a)</php>        <php>~$a</php>                      |                             | 
-<php>'<<'($a, $b)</php>   <php>$a << $b</php>                 ^ +<php>'<<'($a, $b)</php>   <php>$a << $b</php>                 |                             | 
-<php>'>>'($a, $b)</php>   <php>$a >> $b</php>                 ^ +<php>'>>'($a, $b)</php>   <php>$a >> $b</php>                 |                             | 
-<php>'=='($a, $b)</php>   <php>$a == $b</php>                 ^ +<php>'=='($a, $b)</php>   <php>$a == $b</php>                 |                             | 
-<php>'==='($a, $b)</php>  <php>$a === $b</php>                ^ +<php>'==='($a, $b)</php>  <php>$a === $b</php>                |                             | 
-<php>'!='($a, $b)</php>   <php>$a != $b</php>                 ^ +<php>'!='($a, $b)</php>   <php>$a != $b</php>                 |                             | 
-<php>'<>'($a, $b)</php>   <php>$a <> $b</php>                 ^ +<php>'<>'($a, $b)</php>   <php>$a <> $b</php>                 |                             | 
-<php>'!=='($a, $b)</php>  <php>$a !== $b</php>                ^ +<php>'!=='($a, $b)</php>  <php>$a !== $b</php>                |                             | 
-<php>'<'($a, $b)</php>    <php>$a < $b</php>                  ^ +<php>'<'($a, $b)</php>    <php>$a < $b</php>                  |                             | 
-<php>'>'($a, $b)</php>    <php>$a > $b</php>                  ^ +<php>'>'($a, $b)</php>    <php>$a > $b</php>                  |                             | 
-<php>'<='($a, $b)</php>   <php>$a <= $b</php>                 ^ +<php>'<='($a, $b)</php>   <php>$a <= $b</php>                 |                             | 
-<php>'>='($a, $b)</php>   <php>$a >= $b</php>                 ^ +<php>'>='($a, $b)</php>   <php>$a >= $b</php>                 |                             | 
-<php>'<=>'($a, $b)</php>  <php>$a <=> $b</php>                ^ +<php>'<=>'($a, $b)</php>  <php>$a <=> $b</php>                |                             | 
-<php>'&&'($a, $b)</php>   <php>$a <=> $b</php>                ^ +<php>'&&'($a, $b)</php>   <php>$a && $b</php>                 | Can't fully short-circuit.  | 
-<php>'and'($a, $b)</php>  <php>$a and $b</php>                ^ +<php>'and'($a, $b)</php>  <php>$a and $b</php>                | Can't fully short-circuit.  | 
-<php>'||'($a, $b)</php>   <php>$a || $b</php>                 ^ +<php>'||'($a, $b)</php>   <php>$a || $b</php>                 | Can't fully short-circuit.  | 
-<php>'or'($a, $b)</php>   <php>$a or $b</php>                 ^ +<php>'or'($a, $b)</php>   <php>$a or $b</php>                 | Can't fully short-circuit.  | 
-<php>'xor'($a, $b)</php>  <php>$a xor $b</php>                ^ +<php>'xor'($a, $b)</php>  <php>$a xor $b</php>                |                             | 
-<php>'!'($a)</php>        <php>!$a</php>                      ^ +<php>'!'($a)</php>        <php>!$a</php>                      |                             | 
-<php>'.'($a, $b)</php>    <php>$a . $b</php>                  ^+<php>'.'($a, $b)</php>    <php>$a . $b</php>                  |                             |
  
 Since <php>pow()</php> already exists and behaves identically to how <php>'**'()</php> would, <php>'**'()</php> is simply an alias of it. Since <php>pow()</php> already exists and behaves identically to how <php>'**'()</php> would, <php>'**'()</php> is simply an alias of it.
Line 115: Line 115:
 ==== Missing operators ==== ==== Missing operators ====
  
-The table above (likes the patch) currently contains all the operators in the Operators section of the PHP Manual, minus <php>instanceof</php> and the assignment operators. Whether these should have functions too is a matter to debate. FurthermorePHP has some other constructs that could be classed as operators but aren't considered such by the manualA (possibly non-exhaustive) list is:+The table above (like the patch) currently contains all the operators in the [[http://php.net/manual/en/language.operators.php|Operators section of the PHP Manual]], minus <php>instanceof</php>, <php>`backticks`</php> and the assignment operators. Whether these should have functions too is a matter to debate; <php>instanceof</php> doesn't take arbitrary expressions and already has a functional counterpart (<php>is_a</php>)As for the assignment operatorsreferences mean they could be done, but from a functional programming perspective they have limited utility.
  
-  * <php>??</php> (Note that the <php>isset()</php> functionality can'be kept.) +PHP also has some other constructs that could be classed as operators but aren't considered such by the manual. A (possibly non-exhaustive) list is: 
-  * <php>?:</php> (Would this be <php>'?:'($a, $b[, $c])</php> and mean either <php>$a ?: $b</php> or <php>$a ? $b : $c</php>? Straightforward and useful.) + 
-  * <php>@</php> (could not be made a function without changing it to act on a callable, however+  * <php>??</php> (Can'<php>isset()</php>. Can'short-circuit.) 
-  * <php>(int)</php>, <php>(string)</php> etc. (note <php>intval()</php> etc exist, but differently named)+  * <php>?:</php> (Could be <php>'?:'($a, $b[, $c])</php> and map to <php>$a ?: $b</php> or <php>$a ? $b : $c</php> depending on parameter count. Can't short-circuit.) 
 +  * <php>@</php> (Could not be made a function without changing it to act on a callable.
 +  * <php>(int)</php>, <php>(string)</php> etc. (Note <php>intval()</php> etc already exist.)
   * <php>clone</php>   * <php>clone</php>
-  * <php>print</php> (this always returns 1, so we might as well make <php>echo</php> a function too even though it's a statement) +  * <php>print</php> (This always returns 1, so we might as well make <php>echo</php> a function too even though it's a statement.
-  * <php>-></php> (how do you distinguish between properties and methodsare identifiers replaced with strings?) +  * <php>-></php> (How do you distinguish between property lookup and method callsAre identifiers replaced with strings?) 
-  * <php>[]</php> (Array indexing. This has some obvious utility.) +  * <php>[]</php> (Array indexing.) 
-  * <php>()</php> (Function invocation. There's… dubious benefit here, <php>call_user_func</php> exists already.) +  * <php>()</php> (Function invocation. <php>call_user_func</php> exists already.) 
-  * <php>eval</php> (Probably not a good rabbit hole to go down.) +  * <php>eval</php> (Probably not a good rabbit hole to go down, this requires frowned-upon stack gymnastics due to affecting the current scope.) 
-  * <php>include</php>, <php>require</php>, <php>include_once</php>, <php>require_once</php> (Obvious utility.) +  * <php>include</php>, <php>require</php>, <php>include_once</php>, <php>require_once</php> 
-  * <php>yield</php>+  * <php>yield</php> (Like <php>eval</php>, would require dubious stack gymnastics. It is a control-flow expression, not merely manipulating values.)
  
 Of these, <php>-></php>, <php>()</php>, <php>@</php> and <php>eval</php> are the most dubious. Of these, <php>-></php>, <php>()</php>, <php>@</php> and <php>eval</php> are the most dubious.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
-All of these operator functions create no backwards-compatibility break, since they have names that cannot be used for userland functions, and thus they cannot possibly conflict with existing code (except that using exotic extensions like runkit). +All of these operator functions create no backwards-compatibility break, since they have names that cannot be used for userland functions, and thus they cannot conflict with function names in existing code (hypothetically this may not be true if using exotic extensions like runkit). 
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 151: Line 153:
 The existing operators themselves behave the same as ever. The existing operators themselves behave the same as ever.
  
-Quoting function names in function calls is not new, it is a consequence of [[rfc:uniform_variable_syntax|Uniform Variable Syntax]].+Being able to quote function names in function calls (e.g. <php>'+'(1, 1)</php>is not new idea introduced by this RFC, it has been possible since [[rfc:uniform_variable_syntax|Uniform Variable Syntax]] in PHP 7.0.
  
 ===== Future Scope ===== ===== Future Scope =====
Line 175: Line 177:
  
 ===== References ===== ===== References =====
 +  * PHP manual operators section: http://php.net/manual/en/language.operators.php
   * Haskell's infix functions (any normal operator is a function and vice-versa) were an inspiration.   * Haskell's infix functions (any normal operator is a function and vice-versa) were an inspiration.
  
rfc/operator_functions.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1