On Fri, Aug 04, 2017 at 05:45:08PM +0200, hw wrote:
> Paul Johnson wrote:
> > On Thu, Aug 03, 2017 at 08:44:45PM +0200, hw wrote:
> > > 
> > > Hi,
> > > 
> > > suppose I have a class FOO and a class BAR.  The parent of BAR is FOO.
> > > 
> > > I would like FOO to /use/ BAR because BAR has some methods needed by FOO.
> > > BAR is /decended/ from FOO because FOO has many methods needed by BAR.
> > > 
> > > Is this possible, or does it lead to some endless recursion when 
> > > compiling?
> > 
> > You may well find that using roles would provide a better solution than
> > using inheritance.  The methods would wish to call from both classes
> > could be defined in one or more roles.  Roles are more usually called
> > traits in other languages.  You can use roles within Moose or Moo, or by
> > using other CPAN modules.  You can read more about roles/traits at
> > 
> >   https://en.wikipedia.org/wiki/Trait_(computer_programming)
> > 
> Hmm.  I think I almost half-way understand what this is about.  For this
> case, it seems as if I would have FOO and BAR use traits instead of having
> FOO use BAR, and it would be somehow different from using a metaclass FOO
> and BAR descend from, like upside-down inheritance turned upside-down again
> to finally get it right.

That's certainly along the right lines, yes.

> I´m not using Moose or anything yet, though.  I thought it might be better
> to start with something basic and perhaps later upgrade.

That's not a bad idea.  But if this is real work rather than an
exercise, be careful unless you know you will have a chance to rewrite
it after the first version.

> Why do I have to run into such a problem?  I´m just starting to learn this.

You might have a particularly difficult problem to solve.  But if you're
just starting it's more likely that you are not decomposing the problem
as well as you could.

If you don't want to use roles you can get a similar effect using
multiple inheritance, where the contents of the role would be included
in a base class.  But there are reasons why roles are generally
recommended over multiple inheritance.

Paul Johnson - p...@pjcj.net

To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org

Reply via email to