Both sides previous revisionPrevious revisionNext revision | Previous revision |
rfc:operator_functions [2017/09/08 23:53] – expand omissions a bit ajf | rfc:operator_functions [2017/09/22 13:28] (current) – external edit 127.0.0.1 |
---|
<code php> | <code php> |
// Select only the positive numbers | // Select only the positive numbers |
$positiveSubset = array_filters($numbers, partialApply('>', 0)); | $positiveSubset = array_filter($numbers, partialApply('<', 0)); |
</code> | </code> |
| |
| An example working partial application implementation would be: |
| |
| <file php partialApply.php> |
| function partialApply(callable $c, ...$args) { |
| return function (...$args2) use ($c, $args) { |
| return $c(...$args, ...$args2); |
| }; |
| } |
| </file> |
| |
==== Detail ==== | ==== Detail ==== |
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. |
==== 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. <php>instanceof</php> doesn't take arbitrary expressions and already hs a functional counterpart (<php>is_a</php>). As for the assignment operators, references mean they could be done. However, they wouldn't be as useful from a functional programming perspective. | 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 operators, references mean they could be done, but from a functional programming perspective they have limited utility. |
| |
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 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> (Note that the <php>isset()</php> functionality can't be kept.) | * <php>??</php> (Can't <php>isset()</php>. Can't short-circuit.) |
* <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 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, however) | * <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 exist, but differently named) | * <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 methods? are identifiers replaced with strings?) | * <php>-></php> (How do you distinguish between property lookup and method calls? Are 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) ===== |
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 a 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 ===== |
| |
===== 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. |
| |