http://www.perlmonks.org?node_id=812538


in reply to Re: Abstract Methods in Moose
in thread Abstract Methods in Moose

I hadn't seen MooseX::ABC - apparently its author's intent was the same as mine, but there are some limitations in the implementation.

It looks like the existence of the method to be implemented is asserted at class definition time, not during instantiation. As a consequence, the program dies unless the abstract method is implemented in the immediate subclass. So once again, it is not possible to "defer" implementation to an arbitrary child class.
package AbstractWidget; use Moose; use MooseX::ABC; requires 'get_widget_type'; package LessAbstractWidget; use Moose; use MooseX::ABC; extends 'AbstractWidget'; package SnazzyWidget; use Moose; extends 'LessAbstractWidget'; sub get_widget_type{ "BasicWidget" } # BOOM: # AbstractWidget requires LessAbstractWidget to implement get_widget_t +ype

OK, but what you say about "specializing" or extending roles was a big eye-opener for me. I was caught thinking in terms of the Java paradigm, thinking of the Moose Role as something to be added into the base class. Instead, what works is when the Role itself acts as the base class, as you illustrated (and Anonymous Monk hinted at). I'm then free to extend/specialize the Role if I want.

The only thing that seems to suffer a little is the terminology - when I say that SnazzyWidget is with 'AbstractWidget', that implies that Snazzy Widget can in fact exist without Abstract Widget if it wanted to, which isn't true. But I think that's a consequence of this base class / concrete class model I have in my head, and my naming of the packages. If I called AbstractWidget something like Typeable instead - the class names would work together better, I think.

Thanks for your input, and also thanks much for your work on Moose. I've barely scratched the surface but I like what I've found so far. I mostly write throw-away Perl scripts for random ad-hoc data processing tasks. I like to model things in terms of OO, but have often shied away from it because of some of the verbosity and opaqueness (my $self = shift;, bless $self, __PACKAGE__, writing accessors etc.) of Perl 5 OO programming. Moose looks to have made it much more natural to work with objects in Perl 5.

Replies are listed 'Best First'.
Re^3: Abstract Methods in Moose
by stvn (Monsignor) on Dec 13, 2009 at 00:38 UTC
    It looks like the existence of the method to be implemented is asserted at class definition time, not during instantiation. As a consequence, the program dies unless the abstract method is implemented in the immediate subclass. So once again, it is not possible to "defer" implementation to an arbitrary child class.

    Sorry for giving the false info, turns out this is listed as a TODO in the docs, I dove right into the source code to see and misunderstood it. I have notified the author though.

    I think roles is a better fit here, you just need to work out the details of naming (which you are already well on your way to doing). I tend to go with two different role naming approaches; Fooable, when providing mostly just behavior, and WithFoo when providing attributes and accompanying methods. Sometimes for more "interface" type roles I will follow something closer to the normal abstract base class naming conventions (see Forest::Tree and Forest::Tree::Reader, Forest::Tree::Writer, Forest::Tree::Loader, etc).

    -stvn
Re^3: Abstract Methods in Moose
by stvn (Monsignor) on Dec 20, 2009 at 13:33 UTC
    As a consequence, the program dies unless the abstract method is implemented in the immediate subclass.

    Take a look at the Changes file for 0.03, it should work now.

    -stvn