On Tue, 05 Oct 2010 04:31:14 +0100, Richard Lynch <c...@l-i-e.com> 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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to