in reply to Abstract Methods in Moose

Perhaps MooseX::ABC is what you are looking for?

You should be able to extend your abstract base class with other abstract classes too (like you show in your example in here). You should also note that roles can be composed of other roles, so this will do what your looking for:

package WidgetRole; use Moose::Role; requires 'get_widget_type'; package AbstractWidget; use Moose::Role; with 'WidgetRole'; package SnazzyWidget; use Moose; with 'AbstractWidget'; sub get_widget_type{ "BasicWidget" }

-stvn

Replies are listed 'Best First'.
Re^2: Abstract Methods in Moose
by crashtest (Curate) on Dec 12, 2009 at 21:06 UTC

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

    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.

      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
      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