Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^3: $bad_names eq $bad_design

by Aristotle (Chancellor)
on Dec 21, 2002 at 12:50 UTC ( #221619=note: print w/replies, xml ) Need Help??

in reply to Re: Re: $bad_names eq $bad_design
in thread $bad_names eq $bad_design

I'm not sure I'd go with the operator overloading here. It does mean you have to keep a firm grasp on all the interfaces and remember what a variable is supposed to contain. It also makes spotting mistakes harder - if I have the wrong object in $current_token, $current_token->equals($token) may well have Perl complaining it can't find that method, while $current_token == $token will (more or less) quietly do the wrong thing.

I like overloading, but I believe it is very, very dangerous if used too readily. It's great when you're creating a class that shares characteristics with the basic data types, like writing a Math::Complex or something, where $a * $b ends up meaning exactly what I expect. Also, overloading stringification is very handy. I would be really hesitant to use it to represent more arbitrary semantics.

As for your example, I'd probably want something like this:

my $point = new Point(42, 42); my $start_point = $point->copy; $point->add_x(1) while $image->element_at($point)->color == RED; my $end_point = $point->copy; $image->draw_line($start_point, $end_point, BLACK);

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^3: $bad_names eq $bad_design
by BrowserUk (Patriarch) on Dec 21, 2002 at 13:06 UTC

    First, it should really be eq rather than ==, but I'm not yet sure which operators perls overloading can handle, not having made any use of it, but the example was intended as pseudo code rather than anything anyone would implement.

    That aside, I do agree that overloading needs to be used with care, but I wouldn't consider equality testing abitrary semantics. I can however see your point in this regard given a non-implemented method, though it would rapidly show up in testing. The alternative would be to require that objects implement (even if only dummy) methods for the common operators, but that smacks of Java exceptions and is completely against the spirit of Perl.

    You could view this type of failure (or lack of failure:) under the heading caveat implementor as with some much of standard perl stuff.

    I'm not really sure to which extreme I would tend. I need more time using perl and to read more opinions before I could make that choice.

    Examine what is said, not who speaks.

      I'm not yet sure which operators perls overloading can handle

      AFAIK All of them. Even the handling of different quoting types in the code.

      Its a fallback system however. If the operator is explicitly defined then that definition is used, otherwise if overload can syntheisze the behaviour of the opertor in question by using a different defined operator then it will do so. For instance defining '<=>' and 'cmp' with the normal settings (the fallback behaviour is configurable) iirc will cause all of the equivelency operators, numeric and string, behavior to be overriden.

      One thing you have to watch out for however is the common debugging habit of stringifying references by concatenation. The "" operator can cause some suprising results.

      A fairly common use of overload might be

      use overload qw("" stringify <=> num_comparision cmp str_comparison);
      When the right hand side is a string as it is in this case then the handler is expected to be a method of that name called against the object. Otherwise a coderef is expected.

      The overload docs were written by a mathematician which is why they are relatively incomprehensible to mere mortals like us. Its incredibly easy to miss details or become confused when reading that document. I think I've read it at least ten times and I still find things I haven't seen before.

      --- demerphq
      my friends call me, usually because I'm late....

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://221619]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2023-06-04 01:41 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (17 votes). Check out past polls.