Since both you and perrin have responded among similar lines, it seems that my examples are unclear and aren't communicating very well. Let me address your points in order and see if I can clear up my intent.

  1. It is my opinion that Java provides interfaces in lieu of multiple inheritance. I think that's a lousy special case that doesn't address the real issue. (I find myself saying that about Java quite often.)
  2. I agree very much. I think that that addresses the real issue backwards, however. My code shouldn't have to care whether it's dealing with an object that isa specific type. I'd much rather have it check to see that it can act as a specific type.
  3. I disagree that the only way to implement an interface without inheritance is to write a wrapper. I've got a generic mock object that implements any interface you can imagine without wrapping at all. What I lack is a way to say, "Does this object act as if it were an object of this type?"

My goal is twofold:

In other words, I don't want this:

package A::Class; sub some_method { my ($self, $object) = @_; die "Object is not what I expect" unless UNIVERSAL::isa( $object, 'Some::Class' ); $object->some_other_method(); $object->some_method_or_other(); }

That unnecessarily dictates that $object has to inherit from Some::Class, or at least fake up enough so that it can fool UNIVERSAL::isa(). Consider a test case:

use Test::More 'no_plan'; use Test::MockObject; my $mock = Test::MockObject->new(); use_ok( 'A::Class' ); my $a = A::Class->new(); isa_ok( $a, 'A::Class' ); $mock->set_true( 'some_other_method' ) ->set_true( 'some_method_or_other' ); $a->some_method( $mock );

That dies, unnecessarily, because Test::MockObject does not subclass Some::Class. That's not what I want to express, either.

What I really want to express is that $object, whatever it is, ought to act like a Some::Class object. If it inherits from Some::Class, that's fine. If it delegates to a Some::Class object, that's fine. If it contains a Some::Class object, that's fine. If it simply conforms to the Some::Class interface, that's fine.

I don't think "reworking the object model" to make the other three possibilities fit into the inheritance scheme is the right approach. I think the interface approach is more general and, if implemented correctly, can handle all four possibilities easily.

I do think my examples were unclear though, and apologize for the confusion they caused.


In reply to Re: Re: Re: Re: Class::Interface -- isa() Considered Harmful by chromatic
in thread Class::Interface -- isa() Considered Harmful by chromatic

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.