Re: No overload magic

[prev] [thread] [next] [Date index for 2005/02/16]

From: Michael G Schwern
Subject: Re: No overload magic
Date: 11:25 on 16 Feb 2005
On Wed, Feb 16, 2005 at 10:05:41AM +0000, William Ross wrote:
> In the message you cite the author was trying to perform comparisons on 
> cdbi objects and assuming that ($x > $y) is the same as ($x->id > 
> $y->id). There's no reason why that assumption should be correct: not 
> all classes have a single key, and it's just as likely that you would 
> want to compare some other column or a computed value, so cdbi leaves 
> you to specify your own overloading.

I think that's a bit silly. [1]  Its not as likely you want to compare some
other column, you probably want to compare the primary keys especially
when its an equality test.  CDBI can do the right thing by default for
$x == $y even in the case of multiple primary keys.  Its an expectation
that was added and now appears to have been removed?

By default it should stringify to its primary key.  If its used in a
numeric comparision let Perl's string->number comparison take care of it
and let the warnings fall as they may.

In the uncommon case of multiple primary keys it can compare each primary
key in the order it was defined.  The equivalent of:

	my $cmp = 0;
	foreach my $key ($a->columns('Primary')) {
	    # replace cmp with <=> as appropriate
	    $cmp = $a->$key() cmp $b->$key();
	    last if $cmp != 0;
	}
	return $cmp;

This seems to me to be a useful default which allows easy object comparison
and restores backwards compat.  If a user wants something else they can
customize it to their hearts content.


[1] Though I haven't been following the discussion too closely.

No overload magic
Karjala 07:55 on 16 Feb 2005

Re: No overload magic
Karjala 08:01 on 16 Feb 2005

Re: No overload magic
William Ross 10:05 on 16 Feb 2005

Re: No overload magic
Michael G Schwern 11:25 on 16 Feb 2005

Re: No overload magic
Tony Bowden 14:10 on 16 Feb 2005

Re: No overload magic
William Ross 14:20 on 17 Feb 2005

Re: No overload magic
Michael G Schwern 06:43 on 18 Feb 2005

Re: No overload magic
Tony Bowden 08:21 on 18 Feb 2005

Re: No overload magic
Tim Bunce 09:43 on 18 Feb 2005

Re: No overload magic
Karjala 11:57 on 16 Feb 2005

Re: No overload magic
William Ross 13:52 on 16 Feb 2005

Re: No overload magic
Karjala 14:09 on 16 Feb 2005

Re: No overload magic
Michele Valzelli 11:59 on 16 Feb 2005

Re: No overload magic
Ofer Nave 21:53 on 16 Feb 2005

Re: No overload magic
Ken.Olstad 22:00 on 16 Feb 2005

Generated at 11:20 on 20 Feb 2005 by mariachi v0.52