Re: [VOTE] Combined Comparison (Spaceship) Operator
| From: | Levi Morrison | Date: | Mon, 02 Feb 2015 16:06:59 +0000 |
| Subject: | Re: [VOTE] Combined Comparison (Spaceship) Operator | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81592@lists.php.net to get a copy of this message | ||
On Mon, Feb 2, 2015 at 8:27 AM, Andrea Faulds <ajf@ajf.me> wrote:
> Hey Nikita,
>
>> On 2 Feb 2015, at 13:49, Nikita Popov <nikita.ppv@gmail.com> wrote:
>>
>> I've voted -1 because I think this should be a function and not an operator.
>> compare($a, $b) is more obvious than $a <=> $b and it's not like writing comparison
>> functions is such a super common use case that it needs the extra brevity of an operator. A function
>> can furthermore be used as a callback, while an operator requires a wrapping closure.
>
> There’s no actual use for the bare comparison operation as a callback, though: usort($foo,
> ‘compare’); would just be a slow version of sort($foo);
For this particular use-case you are correct, but usually I use custom
callbacks for something more interesting, such as comparing one value
and then comparing another based on the result. Perhaps the callback
is higher-level:
function (callable $comparator) {
return function ($selector) use ($comparator) {
return function ($a, $b) use($selector, $comparator) {
return $comparator($selector($a), $selector($b));
};
};
}
Maybe I'm the only one doing stuff like this, but it's not uncommon for me ^^