Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:49872 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 52346 invoked from network); 5 Oct 2010 10:44:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Oct 2010 10:44:42 -0000 Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.22 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.22 smtp2.ist.utl.pt Linux 2.6 Received: from [193.136.128.22] ([193.136.128.22:57361] helo=smtp2.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 59/21-45586-8910BAC4 for ; Tue, 05 Oct 2010 06:44:41 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp2.ist.utl.pt (Postfix) with ESMTP id 4F666700044E for ; Tue, 5 Oct 2010 11:44:37 +0100 (WEST) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp2.ist.utl.pt ([127.0.0.1]) by localhost (smtp2.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id EVCP0PO7Ebg1 for ; Tue, 5 Oct 2010 11:44:37 +0100 (WEST) Received: from mail2.ist.utl.pt (mail2.ist.utl.pt [IPv6:2001:690:2100:1::c]) by smtp2.ist.utl.pt (Postfix) with ESMTP id F332F7000448 for ; Tue, 5 Oct 2010 11:44:36 +0100 (WEST) Received: from damnation.dulce.lo.geleia.net (52.152.108.93.rev.vodafone.pt [93.108.152.52]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id CAA572001490 for ; Tue, 5 Oct 2010 11:44:36 +0100 (WEST) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: internals@lists.php.net References: <1285856641.16846.92.camel@guybrush> <1286115365.2606.302.camel@guybrush> <1286116661.2606.313.camel@guybrush> Date: Tue, 05 Oct 2010 11:44:39 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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] RFC: Comparable interface From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Tue, 05 Oct 2010 04:31:14 +0100, Richard Lynch wrote: > On Sun, October 3, 2010 12:34 pm, Gustavo Lopes wrote: >> * The current behavior for >, <, etc. is completely useless. It's >> unpredictable and it doesn't even establish a total order: >> >> $a = new stdclass; >> $a->prop = null; >> $b = new stdclass; >> $b->prop2 = null; >> >> var_dump($a > $b); //false >> var_dump($a == $b); //false >> var_dump($b > $a); //false > > Errrr. > > Which one of these would you expect to be "true"?... > > They're sure not equal, right? > > And what would make $a or $b "greater" than the other? I mean, *WHY* > would you expect one of those to be "true", if you do expect one of > them to be "true"? > > I sure can't find any logical a priori ordering, total, partial, or > otherwise. > > Last time I checked, there was no "rule" that any set/graph or other > mathematical collection had to have a defined ordering, even partial. > > But it's been ages since I got my Honors Math degree, so maybe they > changed the rule while I wasn't looking... :-) > Without addressing the appeal to authority argument, the problem is not that there "must" be an ordering, partial or total. The problem is that several algorithms expect a total order criterion to exist; a notable example are sorting functions. While many will still terminate when given a partial order, the results will be unpredictable. So basically you have comparison function for objects, but you cannot, in general, use it to sort even objects of the same class (seem my example). Note that e.g. Java and .NET require a total order, see: http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx http://download.oracle.com/javase/6/docs/api/java/lang/Comparable.html#compareTo(T) It would be somewhat useful to be able to sort objects by some (arbitrary) total ordering criterion, e.g. to do a binary search afterwards, but that's marginal to my argument, which is that the current object comparison behavior ranges from limited to useless. Therefore, some addition that would allow the user to give meaningful behavior to the operators in some limited circumstances (e.g. comparisons inside classes with inheritance relationships only) would be a good idea. -- Gustavo Lopes