Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Class::Interface -- isa() Considered Harmful

by bart (Canon)
on Jan 16, 2003 at 04:35 UTC ( [id://227334]=note: print w/replies, xml ) Need Help??


in reply to Class::Interface -- isa() Considered Harmful

You don't need isa, you need can.

I often write code of which I assume that an object is able to execute certain methods. When in doubt, I try can() first, before actually calling the method. These classes do not need to be derived from a common class, that is an implementation detail to make virtual classes possible in languages like C++, for example. In Perl, when classes don't have common methods, it doesn't make sense to make them inherit from a common ancestor class.

I do assume that for those classes, methods of the same name serve a similar purpose, and have the same API. That may be a weak point. Likely so.

  • Comment on Re: Class::Interface -- isa() Considered Harmful

Replies are listed 'Best First'.
Re: Re: Class::Interface -- isa() Considered Harmful
by dws (Chancellor) on Jan 16, 2003 at 07:37 UTC
    You don't need isa, you need can.

    An interface is a contract. Having a method with a given name isn't. Keeping the conversation at the level of "will this object honor a given contract" (i.e., isa) is preferable to risking false positive based on method names. A baseball player and an airplane both know about "pitch", but the meanings are entirely different.

Re: Re: Class::Interface -- isa() Considered Harmful
by chromatic (Archbishop) on Jan 16, 2003 at 07:42 UTC

    You're right that can() is a better approach for protecting against methods that aren't there. I'm working on something at a higher level, though. For example, you don't have to check can() all the time. If you're handed an object, check that it implements the appropriate interface, then keep it around.

    With interfaces, for example, it's easy to mixin a batch of methods from another class -- marked as an interface.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-23 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found