Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:72589 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 2785 invoked from network); 14 Feb 2014 10:45:04 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 14 Feb 2014 10:45:04 -0000 Authentication-Results: pb1.pair.com header.from=julienpauli@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=julienpauli@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.173 as permitted sender) X-PHP-List-Original-Sender: julienpauli@gmail.com X-Host-Fingerprint: 209.85.220.173 mail-vc0-f173.google.com Received: from [209.85.220.173] ([209.85.220.173:38660] helo=mail-vc0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 03/73-12906-FA3FDF25 for ; Fri, 14 Feb 2014 05:45:03 -0500 Received: by mail-vc0-f173.google.com with SMTP id ld13so9025484vcb.4 for ; Fri, 14 Feb 2014 02:45:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=Q2McbEBYiF4d/m8ex3ZUVLzfNpL0ZzekRKadS1Ph5Wk=; b=njuPNWVGCaY454To7k2AFRLAfxHGbuBe4IMjKhjHnPnLviCxykuL1FiYUlk63Q2xjI UNJwMk74fMLedI5qNCKB+EZe2Mr0x347mNwXCl3JyRtSyhEcy27gri7kELSA+WxRn2gz tsedfkbxu+9WyQCSvDMcDnIWHEubPvuZ5Wmj2IzwGRmKfr+n7gCEJOBoRk3yK1wAuG/O uVCcrIfOmNWGw47EADZ1Ex6PR3HjJcktV+Ac1vwXl4OEnsyTmOlV4iYkXr886h4AW5Uk q77mB8LElLkqKAL0vl9GZZJRws0+9XLUQKe1NNAqq5puStffa71luULYW19fJvvqzLlE JNJQ== X-Received: by 10.52.165.101 with SMTP id yx5mr591221vdb.29.1392374700797; Fri, 14 Feb 2014 02:45:00 -0800 (PST) MIME-Version: 1.0 Sender: julienpauli@gmail.com Received: by 10.221.8.129 with HTTP; Fri, 14 Feb 2014 02:44:20 -0800 (PST) In-Reply-To: <52FD0932.8040900@php.net> References: <4ED7146272E04A47B986ED49E771E347BBC71EE997@Ikarus.ameusgmbh.intern> <52FCE4FC.6010204@php.net> <52FD0932.8040900@php.net> Date: Fri, 14 Feb 2014 11:44:20 +0100 X-Google-Sender-Auth: sr3Yx-E1-gdG_BJkafjEwCuQox0 Message-ID: To: Davey Shafik Cc: PHP Internals , Pierre Joye , Simon J Welsh , Christian Stoller Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] [RFC] Combined Comparison Operator From: jpauli@php.net (Julien Pauli) On Thu, Feb 13, 2014 at 7:04 PM, Davey Shafik wrote: > On 2/13/14, 12:14 PM, Pierre Joye wrote: >> >> Hi David, >> >> Thanks for this proposal. >> >> On Feb 13, 2014 10:30 PM, "Davey Shafik" wrote: >>> >>> >>> On 2/13/14, 2:19 AM, Christian Stoller wrote: >>>> >>>> >>>> Davey Shafik wrote: >>>>> >>>>> >>>>> This adds a new operator "(expr) <=> (expr)" that returns 0 if both >>>>> operands are equal, 1 if the left is greater, and -1 if the right is >>>>> greater. >>>>> >>>>> It works with all types (just as well as <, <=, >=, > work) and is >>>>> great >>>>> for usort() callbacks for example. >>>>> >>>> >>>> Why not using `$a - $b` instead of `$a <=> $b`? >>>> >>>> >>>> Simon J Welsh wrote: >>>>> >>>>> >>>>> 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're sorting objects, you probably want to be doing >>>>> the comparison on some string/numeric property. >>>>> >>>> >>>> Agreed! It would make more sense to write a comparison function for >> >> arrays/objects. >>>> >>>> >>>> >>>> Best regards >>>> Christian >>>> >>> >>> This operator DOES work on arrays/objects. The fact it also works on >> >> scalar values is a bonus if that's how you want to look at it :P >> >> This new operator doea not look too useful to me but here is one comment >> about objects handling: >> >> It compares only values, seems to be the only solution but the actual >> usage >> for that is rather inexistent as it compares apples and pears, or am I >> missing something? >> >> Cheers, >> Pierre >> > > So, this behavior can definitely be changed -- preserving the current > behavior of == for the 0 value, and then we can go two different ways for > 1/-1: > > - Compare only like-for-like keys, ignoring extra keys > - Compare like-for-like keys, and count the number of additional keys are > being great. So: if they match on all common keys, but left has 3 additional > keys, and right has 4, right is greater than left. > > It's definitely tough to get the semantics right. But: the behavior is also > currently identical to $obj <= $obj2, etc. I felt that consistency was most > important. > > The logical next step with this operator is to add a "Comparable" > "interface" like ruby (not saying it has to be an interface, magic method > also works), that allows the objects themselves to define the comparison > behavior, so you could do: > > class Foo { > function __compare($right) { > // logic here > } > } > > $left = new Foo; > $right = new Foo; > > $left <=> $right; // Same as $left->__compare($right); Please, don't. Comparable has already been discussed many times, and has its own RFC and topic. https://wiki.php.net/rfc/comparable Don't mix things. Comparable is another idea than adding a new operator. Julien