Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72544 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24089 invoked from network); 13 Feb 2014 05:07:31 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Feb 2014 05:07:31 -0000 Authentication-Results: pb1.pair.com header.from=simon@simon.geek.nz; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=simon@simon.geek.nz; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain simon.geek.nz designates 103.6.213.4 as permitted sender) X-PHP-List-Original-Sender: simon@simon.geek.nz X-Host-Fingerprint: 103.6.213.4 jabber.simon.geek.nz Received: from [103.6.213.4] ([103.6.213.4:45687] helo=simon.geek.nz) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 11/C0-18732-0135CF25 for ; Thu, 13 Feb 2014 00:07:30 -0500 Received: from yogurt.fritz.box (office.pocketrent.com [203.86.207.220]) by simon.geek.nz (Postfix) with ESMTPSA id 631A860430; Thu, 13 Feb 2014 18:07:24 +1300 (NZDT) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 7.1 \(1827\)) In-Reply-To: <52FC4C8B.1040303@php.net> Date: Thu, 13 Feb 2014 18:07:19 +1300 Cc: "internals@lists.php.net Internals" , Yasuo Ohgaki Content-Transfer-Encoding: quoted-printable Message-ID: References: <52FC4C8B.1040303@php.net> To: Davey Shafik X-Mailer: Apple Mail (2.1827) Subject: Re: [PHP-DEV] [RFC] Combined Comparison Operator From: simon@simon.geek.nz (Simon J Welsh) On 13/02/2014, at 17:39, Davey Shafik wrote: > On 2/12/14, 11:25 PM, Yasuo Ohgaki wrote: >> Hi Davey, >>=20 >> On Thu, Feb 13, 2014 at 12:58 PM, Davey Shafik wrote: >>=20 >>> I've written up an RFC/Patch to gauge interest and get feedback on = the >>> addition of a combined comparison (aka: spaceship) operator. >>>=20 >>> You can see the RFC at: https://wiki.php.net/rfc/ >>> combined-comparison-operator >>>=20 >>> This adds a new operator "(expr) <=3D> (expr)" that returns 0 if = both >>> operands are equal, 1 if the left is greater, and -1 if the right is >>> greater. >>>=20 >>> It works with all types (just as well as <, <=3D, >=3D, > work) and = is great >>> for usort() callbacks for example. >>>=20 >>> Code available here (against 5.6): https://github.com/dshafik/ >>> php-src/compare/add-spaceship-operator >>>=20 >>> I'd love to get this into 5.6, not sure if we hit feature freeze = yet. >>> Also, not sure if it needs 2/3 majority, but assumed so as pow did? >>>=20 >>> Thoughts? >>>=20 >>> If there is interest, I'll start adding tests. They should be fairly >>> trivial. >>>=20 >>=20 >> Interesting shortcut operator. >> I feel closure function is for the task. >> I would vote 0, but close to -1 since it's only useful for sorting. >> Is there any other use cases? >>=20 >> Regards, >>=20 >> -- >> Yasuo Ohgaki >> yohgaki@ohgaki.net >>=20 >=20 > Yasuo, >=20 > Sorting is definitely it's most compelling use. It cuts down on = potentially quite a bit of code: >=20 > function ($left, $right) { > if ($left[1] =3D=3D $right[1]) { > return 0; > } >=20 > if ($left[1] > $right[1]) { > return 1; > } >=20 > if ($left[1] < $right[1]) { > return -1; > } > } >=20 > or (as per the RFC): >=20 > function ($left, $right) { > return $left[1] <=3D> $right[1]; > } >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php The only real case I see for this is to save some boilerplate when = dealing with arrays. Strings have strcmp(), numbers have subtraction and = when you=92re sorting objects, you probably want to be doing the = comparison on some string/numeric property. If you really need the -1/0/1 return value (sorting functions don=92t), = then wrap the strcmp/subtraction in a trivial sign() function. --- Simon Welsh Admin of http://simon.geek.nz/