Hi,

I have a Moose-question:

Previously I have used Class::Std and one of the features I like about it is that there are not attribute-collisions betweeen a class and it's base-classes.

Now what happens in Moose when there are collisions? Let's try:

package Hubba; use Moose; has hubba => ( is => "rw", isa => "Int", builder => "hubba_builder1" ) +; sub hubba_builder1 { return 1; } sub abba { my($this)=@_; return $this->hubba + 1; }

This is a class that has an int-attribute "hubba" together with a builder and another method "abba" that uses that attribute. Now we add a collision:

package Bubba; use Moose; extends "Hubba"; has hubba => ( is => "rw", isa => "Str", builder => "hubba_builder2" +); sub hubba_builder2 { my($this)=@_; return "bubba"; }

So now we have a derived class that also uses a "hubba"-attribute but with a different builder and a different type.

The problem (at least I consider it a problem) now appears when we do this:

Hubba->new->abba;

Which in this example produces a warning as abba expects an int but gets a string.

Please forgive the clumsy example but the problem I see is this:

I (up to now) have held the conviction that you should be able to derive from a class without knowing it's implementation (or attribute-list) and by deriving you should not break base-class functionality but this does not be possible in Moose. As I have tried to show above a class that derives from a base-class but accidentaly uses an attribute with the same name as one of the parent-classes not only shadows the parent-class attribute but completely replaces it, thereby possibly damaging base-class methods.

So below the line it looks to me that whenever I use Moose and want to derive from a class I have to manually ensure that there are no attribute-collision - or am I doing something wrong?

Many thanks!


In reply to attribute collisions ín Moose by morgon

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.