On Mon, Sep 25, 2006 at 11:10:41AM -0400, John Peacock wrote:
Rick Delaney wrote:
I'm a bit surprised that cmp generates eq without specifying fallback.
I don't a conceptual problem with that; 'cmp' is the same operation as
'eq', 'lt', and 'gt' (all at once). Similarly, '<=>' is '==', '<', and
This is so if you want your operators to have similar meanings to the
standard ones. But there is no reason why one might not want to overload
<=> to mean something completely different, maybe a bizarre form of
assignment operator since you can't overload '='.
However, it would be wrong for 'stringify' to be used to do some
comparative operation without asking (i.e. without fallback). As a
thought experiment, imagine an overloaded number class where the
stringify would spell out the number (I18N'd of course), 'cmp' would
throw an error, and '<=>' would operate on the numeric value. The
"value" produced by stringify is in no way comparable here.
That may be true for lt and gt, although I think one could argue that
this would just provide a stringy comparison alternative, which, while
probably not very useful, would still be logical. Regardless, if two
objects are equal then they will probably stringify to the same thing
and so an automatic fallback for 'eq' would be as reasonable as the
automatic fallback to 'lt' from 'cmp'.
I am not advocating changing anything, just pointing out some
inconsistencies. And I was only a *bit* surprised because I've come to
expect inconsistent and unclear (poorly documented) behaviour from
overload, especially wrt fallback.