Pedro Larroy writes:
> Would it be a good idea to make ==, and other numeric comparators
> polymorphic so they can be used also for string comparisons? Or the
> will is to keep eq, gt and the others. (not very nice emho).

It was decided long ago that the distinction between == and eq is going
to stay.  En mi humilde opiniÃn, the idea is not archaic, but quite
modern.  Or postmodern Larry would say, though I don't really understand
what that means.

Since scalars are polymorphic, their operations must not be.  The
problem is the same as that of using + for string concatenation (which
has been proposed far too many times).  A big part of Perl is the
ability to do:

    my $a = 43;
    $a += 1;
    $a ~= " bananas";   # 44 bananas

And the ability to keep the semantics the exact same if you happen to
put quotes around the "43"... or read it in from a file, for instance.

Admittedly, if you use == for everything, you can force string or
numeric comparison this way:

    if +$a == +$b {...}   # numeric
    if ~$a == ~$b {...}   # string

But it's been pointed out:

    if +(some($big+%nastyÂexpressionÂ)) == +$b {...}

Now it takes three, maybe four re-scans to figure out that it's a
numeric comparison.  Kills readability.

Fortunately for the generic programmer there's a generic equality which
returns true when objects are the same type (or almost the same, like an
integer and a string that looks like an integer), and false otherwise.
Larry called it "equal", though "equals" seems much, much better.

    if $a equals $b {...}

There's your "polymorphic" equality, if you will.  I'd be wary of using
it often, however.

Luke

Reply via email to