Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49836 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95865 invoked from network); 1 Oct 2010 09:27:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Oct 2010 09:27:51 -0000 Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.21 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.21 smtp1.ist.utl.pt Linux 2.6 Received: from [193.136.128.21] ([193.136.128.21:44193] helo=smtp1.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5C/2B-18648-399A5AC4 for ; Fri, 01 Oct 2010 05:27:49 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id C4C6170003C7 for ; Fri, 1 Oct 2010 10:27:44 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp1.ist.utl.pt ([127.0.0.1]) by localhost (smtp1.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id 0Ie22WK6dQjj for ; Fri, 1 Oct 2010 10:27:44 +0100 (WEST) Received: from mail2.ist.utl.pt (mail2.ist.utl.pt [IPv6:2001:690:2100:1::c]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 6220670003C3 for ; Fri, 1 Oct 2010 10:27:44 +0100 (WEST) Received: from damnation (a79-168-249-157.cpe.netcabo.pt [79.168.249.157]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id C4822200798B for ; Fri, 1 Oct 2010 10:27:43 +0100 (WEST) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: internals@lists.php.net References: Date: Fri, 01 Oct 2010 10:27:51 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: Quoted-Printable Organization: =?utf-8?Q?N=C3=BAcleo_de_Eng=2E_Biom=C3=A9di?= =?utf-8?Q?ca_do_IST?= Message-ID: In-Reply-To: User-Agent: Opera Mail/10.62 (Win32) Subject: Re: [PHP-DEV] Re: RFC: Comparable interface From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Fri, 01 Oct 2010 09:38:19 +0100, Adam Harvey wrote:= > On 30 September 2010 21:33, Adam Harvey wrote: >> I've just written an RFC (with a patch against trunk) to implement a >> Comparable interface similar to that in Java =E2=80=94 in effect, all= owing >> object instances to be compared with semantics defined in userspace. >> This is admittedly at the lower end of RFC-worthy proposals, but it's= >> a good system, and I'd like to see it used a little more. Plus, it's >> good practice for the more interesting stuff to come. :) > > I've updated the RFC with a fresh patch that doesn't involve SPL at > all and attempted to incorporate some of the questions and answers so > far into the RFC. I've also added a section for concerns with the RFC;= > I've attempted to distil the concerns I've seen so far, but please > feel free to edit that list if specific concerns are misrepresented or= > unrepresented. > > Links: > > RFC: http://wiki.php.net/rfc/comparable > Patch v2: http://www.adamharvey.name/patches/comparable-v2.diff.txt > Here's a new concern: I find a bad idea to call compareTo *before* = 'compare_objects'. This means that if in your internal class you define = = your own comparison logic with 'compare_objects', you cannot make sure t= he = userland subclasses won't completely replace your logic by implementing = = Comparable. So to prevent this you'd have to implement 'Comparable' = yourself... The low level feature should have priority (look at e.g. Countable vs = count_objects); you could always in your 'compare_objects' check for the= = interface and call it if you want to allow userspace subclasses to repla= ce = the comparison logic. The RFC says: >> How do you interact with 'get' and 'cast_object'/'__tostring'? > The tests for whether the operands implement Comparable occur before > any potential calls to get and cast_object. So basically this means that 'get' and 'cast_object' will never be calle= d = of an object $a will never be called if it's being compared to a = Comparable object $b, right? This is not a very good idea because it puts the responsibility to deal = = correctly with proxy objects and scalar castable objects on the part of = = the implementor of the compareTo method. Tis means people implementing compareTo natively will almost never get i= t = right. Let's say they want to make their object comparable with integers= . = You pass a proxy object that yields integers. Now the native implementor= = sees an object and must remember to check for the existence of 'get' and= = 'cast_object' and call them if necessary. Right. For userland implementations, the situation is not much better; the = implementor for compare would (most likely) have to do an explicit cast = to = the type he wants to compare with and, of course, using stuff like = 'gettype' would probably result in wrong behavior. TL;DR: 'get' and 'cast_object' should be checked for and, if necessary, = = called first. >> If both $a and $b are objects with different compare functions, >> how it is determined whose function is used? Note that operators >> like =3D=3D are assumed to be commutative, and less/more operators >> are assumed to be commutative in pairs, like above. > The left operand wins, so $a. At least in Java, it's clear this is happening because you call = a.compareTo(b). This is a dangerous feature... In my opinion, if they ha= ve = different compareTo implementations, they shouldn't be compared. -- = Gustavo Lopes