Since I'm new to this and haven't tried it out yet: can a class that consumes a trait override the code it receives from the trait?

Yes. I can simply choose not to import a particular method. Using Class::Trait syntax:

use Class::Trait some_trait => { exclude => [ "methods_I_dont_want" ] };

If you still need that code and don't want to exclude it, you can alias it to another name:

use Class::Trait some_trait => { alias => { old_name => 'new_name' } };

Apart from moving them to deterministic compile-time, how do traits help resolve ordering problems associated with MI?

Let's say that you have a class which inherits from both a Bomb class and a GirlFriend class.

package SomeThingOrOther; use base qw/GirlFriend Bomb/; ...

(I realize you understand the ordering problem. I explain the following for the benefit of those who might be reading and not know.) Your class needs to explode() from Bomb, but unbeknownst to you, the GirlFriend class also has an explode() method! Now, because you used GirlFriend first in your use base ... statement, you get the wrong behavior and this can be very difficult to debug. Of course, if there's another duplicate method in your two base classes but you don't want the one in your Bomb class, you're life gets even more difficult. Now you need to use delegation or start hard coding classnames, neither of which is quite as easy as simply inheriting the data was supposed to be.

Traits make this a trivial problem:

use Class::Trait GirlFriend => { exclude => [ "explode" ] }; use Class::Trait Bomb => { exclude => [ "some_method_in_girlfriend" ] };

Suppose I have class Base that uses traits. Then along comes class Child. Where does it get its traits from?

The Child class should not know or care about where Base gets its methods. Are they traits or implemented directly? It should not matter. All Child needs to know is the published interface to Base. That's when the Child class can decide whether or not to override Base methods. Whether this is done through writing the methods directly or use of traits should not matter at this point.

One final comment: some of my code snippets above can look daunting to those unfamiliar with traits. In reality, most traits are pretty straightforward.

use Class::Trait 'TPrintable'; use Class::Trait 'TId';

Hope this helps!

Cheers,
Ovid

New address of my CGI Course.


In reply to Re^3: interface.pm explained by Ovid
in thread interface.pm explained by gaal

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.