EvanCarroll

This is a common problem with parrallel class heirarchies, and one which no one has managed to solve well. It is sometimes referred to as the "Expression Problem", here is a paper which describes how it is solved in Scala using Traits. However, I don't think this particular solution would be possible using Moose.

The Moose-ish solution would be as follows:

package EvanCarroll::Quiz; use Moose; has 'question_class' => (is => 'ro', default => 'EvalCarroll::Question +'); sub new_question { my $self = shift; my $question = $self->question_class->new(@_); push @{ $self->questions }, $question; $question; }
Now, at this point you can do a number of things. The first one being using the constructor parameters for Quiz like so:
my $quiz = EvanCarroll::Quiz->new( question_class => 'My::Custom::Question' );
From what I understand of your design, this code would go into QuizMaster::Custom, which I assume would know what custom question class to use before it created the Quiz instance.

Of course you can also do the same in QuizMaster with the Quiz class, and get that to be dynamic as well.

Now if you want to allow the author of a custom Quiz subclass to supply an arbitrary Question class, and not enforce namespace consistency across Quiz and Question, then your user could just do this:

package Stvn::Quiz; use Moose; extends 'EvalCarroll::Quiz'; has '+question_class' => (default => 'Some::Random::Question');
The '+' in front of the attribute name tells Moose to clone the attribute from it's superclass (in this case EvanCarroll::Quiz) and to change just the 'default' value for question_class to be 'Some::Random::Question'.

And of course, this does not prevent your Quiz::Master class from still forcing the question_class parameter in the Quiz constructor (as shown above). Instead it only creates a new default for the new Quiz subclass.

Hope this helps.

-stvn

In reply to Re: Calling chains, extends, and subclassing by stvn
in thread Calling chains, extends, and subclassing by EvanCarroll

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.