Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81599 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57035 invoked from network); 2 Feb 2015 16:35:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Feb 2015 16:35:34 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.199 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.199 imap11-2.ox.privateemail.com Received: from [192.64.116.199] ([192.64.116.199:48825] helo=imap11-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 96/74-34915-657AFC45 for ; Mon, 02 Feb 2015 11:35:34 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 095548800EF; Mon, 2 Feb 2015 11:35:32 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap11.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap11.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id DKHBsIRgeNhJ; Mon, 2 Feb 2015 11:35:31 -0500 (EST) Received: from oa-res-26-240.wireless.abdn.ac.uk (oa-res-26-240.wireless.abdn.ac.uk [137.50.26.240]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 5C0808800DA; Mon, 2 Feb 2015 11:35:31 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) In-Reply-To: Date: Mon, 2 Feb 2015 16:35:29 +0000 Cc: PHP Internals List Content-Transfer-Encoding: quoted-printable Message-ID: References: <1D2674AE-6DA6-42A4-817E-0166F30B667D@ajf.me> To: Nikita Popov X-Mailer: Apple Mail (2.2070.6) Subject: Re: [PHP-DEV] [VOTE] Combined Comparison (Spaceship) Operator From: ajf@ajf.me (Andrea Faulds) Hey Nikita, > On 2 Feb 2015, at 16:32, Nikita Popov wrote: >=20 > On Mon, Feb 2, 2015 at 4:27 PM, Andrea Faulds wrote: > Hey Nikita, >=20 > > On 2 Feb 2015, at 13:49, Nikita Popov 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 <=3D> $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. >=20 > There=E2=80=99s no actual use for the bare comparison operation as a = callback, though: usort($foo, =E2=80=98compare=E2=80=99); would just be = a slow version of sort($foo); >=20 > It may not be applicable to a direct sort() call, but it's useful for = higher level APIs. With a function you can create APIs of the form = `function foo(..., $comparator =3D 'compare') { ... }`, which will = default to the "standard" comparison function without having to = specially handle it all over the place. Yes, that=E2=80=99s true. I=E2=80=99d previously suggested adding a function to get a closure of = an operator. But actually, perhaps we should just follow PHP=E2=80=99s = traditional string typing of callables, and make =E2=80=98+=E2=80=99, = =E2=80=98-=E2=80=98, =E2=80=98<=E2=80=98, =E2=80=98<=3D>=E2=80=99 etc. = be valid callables? Thus: function doesThing($a, $b, callable $callback =3D =E2=80=98+=E2=80=99)= { return $callback($a, $b); } doesThing(1, 2); // 3 Thoughts? (I realise this is slightly OT.) -- Andrea Faulds http://ajf.me/