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

From: Date: Sun, 13 Feb 2022 02:03:15 +0000
Subject: Req #66368 [Com]: Operators should also be functions
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-239715@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66368&edit=1

 ID:                 66368
 Comment by:         master dot training365 at gmail dot com
 Reported by:        chriswarbo at gmail dot com
 Summary:            Operators should also be functions
 Status:             Assigned
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   Irrelevant
 PHP Version:        Irrelevant
 Assigned To:        ajf
 Block user comment: N
 Private report:     N

 New Comment:

https://pastelink.net/sd8e3bqt


Previous Comments:
------------------------------------------------------------------------
[2022-02-13 01:57:21] master dot training365 at gmail dot com

http://git.province.namur.be/Wikiew/Techi/wiki/Creat5

------------------------------------------------------------------------
[2017-09-12 13:52:28] cmb@php.net

FTR: a respective "Operator functions" RFC is currently under
discussion: <https://wiki.php.net/rfc/operator_functions>.

------------------------------------------------------------------------
[2014-01-02 12:23:24] chriswarbo at gmail dot com

It is sufficient to do:

$o = function($l, $r) {
  return $l + $r;
};

But after defining such a function for the umpteenth time, I decided to raise this issue. I
don't think I'm alone in using such definitions, since it's a common-enough pattern
that we had array_sum built in to the language even before we had lambdas and array_reduce
(array_sum is just array_reduce curried with $o and 0).

One solution would be to build in functions like $o to prevent the need to define them over and over
in-line or in libraries, but I'd rather not be 'that guy' who wants his own
helper-functions built-in, since adding new functions bloats the language and, more importantly,
burdens developers with extra complexity ("Should I use + or $o?").

Instead, I think it's an opportunity to fix the treatment of operators, especially since
functions like $o are just eta-expansions of operators, and eta-expansion is always a useless
transformation:

function eta($f) {
  return function ($x) {
    return $f($x);
  };
}

call_user_func(eta($x), $y) === call_user_func($x, $y)

------------------------------------------------------------------------
[2014-01-01 14:43:23] krakjoe@php.net

Ok, I understand a bit clearer what you're asking for ... but don't really see why; I
cannot imagine a time where:

$o = '+';
$o(10, 20);

Is required such that:

$o = function($l, $r) {
  return $l + $r;
};

Does not suffice.

Maybe someone else will get it, I don't ...

------------------------------------------------------------------------
[2014-01-01 13:04:19] chriswarbo at gmail dot com

As for "[]" and "->" being functions (or function-like), consider the
following:

function subscript($array, $index) {
  return $array[$index];
}

array_map('subscript', $my_arrays, range(0, 9));

function prop($object, $property) {
  return $object->$property;
}

array_map('prop', $my_objects, $props);

Personally I don't use the above functions, but I make heavy use of a function
"lookup" which does both (based on the argument's type) plus a few bells and whistles
(recursively looking up an array of identifiers, exception and error handling for magic methods,
etc.).

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=66368


--
Edit this bug report at https://bugs.php.net/bug.php?id=66368&edit=1


Thread (21 messages)

« previous php.bugs (#239715) next »