Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

What is the difference between a Role and Trait? I've read the CPAN docs and even some of the academic papers trying to get my head wrapped around this stuff.

Reason being I'm active in the MTOS (Movable Type Open Source) community and the discussion about "whatever-able" classes comes up occasionally and whose use is growing.

Let me explain what I mean by "whatever-able" classes in how they are used in MT.

It started off as Taggable (object can be tagged), then Scorable (object can be rated or scored) was introduced and now there is discussion of others like comments and categories. The thing is these "whatever-able" classes are designed that your data object must inherit them which usually means multiple inheritance. It also means only the developer of the object class has the option of making an it taggable etc. So if I'm doing some consulting and the client needs a blog object to be taggable and it's not (not yet so far) I'm out of luck or I could trying some nasty hacking that is sure to break later when new versions are released.

After reading about roles and traits it seems one of these approaches is the saner/better way to go especially since modularity and flexibility and extensibility are cornerstones of MT going forward.

Here are some samples classes from MT to give you some code to look at:


So I need a sanity check and thought I'd post something here. Correct me if I'm wrong.

  • Would what MTOS is calling taggable, scorable qualify as a role or trait? I'm thinking yes, but the examples I've seen don't quite fall inline.
  • Roles seem to be a more simple and basic implementation of the traits idea.
  • Traits includes more advanced features like overloaded operators, required method labels, conflict resolution, aliases and summation.
  • Roles will be built-in to Perl 6. Traits will not.
  • If I wanted to implement roles in MT which CPAN module should I use? Class::Role, Class::Roles, Perl6::Roles? I'd assume the latter given the dates, docs and Perl6 namespace.
  • If Perl6::Roles is the answer to the above, how up to date is it in terms of the current Perl6 implementation?

Your feedback would be appreciated. Thanks.

In reply to Sanity Check: Roles vs. Traits by tima

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2023-12-04 23:39 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (25 votes). Check out past polls.