Req #66368 [NEW]: Operators should also be functions

From: Date: Mon, 30 Dec 2013 08:30:36 +0000
Subject: Req #66368 [NEW]: Operators should also be functions
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-183494@lists.php.net to get a copy of this message
From:             chriswarbo at gmail dot com
Operating system: Irrelevant
PHP version:      Irrelevant
Package:          *General Issues
Bug Type:         Feature/Change Request
Bug description:Operators should also be functions

Description:
------------
PHP's operators (+, -, *, /, ||, &&, etc.) can be tedious to use, since
they are second-class citizens compared to functions. They can't be
abstracted (example 1), can't be used as callbacks (example 2) and must
be hard-coded (example 3).

Operators with non-symbolic names, eg. "or", "and", "xor", etc. can be
defined as functions right now without breaking existing code, since
their names look like valid functions and are guaranteed to be available
(example 4). For consistency, it may be desirable to create synonyms for
operators with symbolic names, eg. "+", "-", "*", etc. (example 5).
Ambiguities can be handled trivially (example 6). Alternatively, the
symbolic names could be used directly (example 7).

In every project I work on, I inevitably end up writing these as in-line
lambdas, then eventually refactor them out into a library. I'm sure I'm
not alone. If PHP stopped treating operators differently from functions,
large amounts of boilerplate code could be scrapped and the redundant
effort of wrapping these in lambdas over and over again could be spared.

Test script:
---------------
$f = 'or';
$example_1 = $g($g(TRUE,
                   $g(FALSE, TRUE)),
                FALSE);

$example_2 = array_reduce(array(TRUE, FALSE), $f, FALSE);

$example_3 = call_user_func(
  function($op) {
    return $op(TRUE, FALSE)
  },
  $f);

// Example 4
function or($x, $y) { return $x or $y; }

// Example 5
function mult($x, $y) { return $x * $y; }

// Example 6
function plus($x, $y = NULL) { return is_null($y)? +$x : $x + $y; }
function minus($x, $y = NULL) { return is_null($y)? -$x : $x - $y; }

$example_7 = call_user_func('+', 10, 5);

Expected result:
----------------
$example_1 == TRUE;
$example_2 == TRUE;
$example_3 == TRUE;
or(TRUE, FALSE) == TRUE;
mult(5, 3) == 15;
plus(10) == 10;
plus(4, 3) == 7;
minus(5) == -5;
minus(8, 7) == 1;
$example_7 == 15;

Actual result:
--------------
$example_1 -> Undefined function 'or'
$example_2 -> Undefined function 'or'
$example_3 -> Undefined function 'or'
$example_4 -> Unexpected T_LOGICAL_OR
$example_7 -> Warning: First argument is expected to be a valid callback

-- 
Edit bug report at https://bugs.php.net/bug.php?id=66368&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=66368&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=66368&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=66368&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=66368&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=66368&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=66368&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=66368&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=66368&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=66368&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=66368&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=66368&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=66368&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=66368&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66368&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=66368&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=66368&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=66368&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66368&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=66368&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=66368&r=mysqlcfg



Thread (21 messages)

« previous php.bugs (#183494) next »