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:
- http://code.sixapart.com/svn/movabletype/branches/mt4.21/lib/MT/Entry.pm
- http://code.sixapart.com/svn/movabletype/branches/mt4.21/lib/MT/Taggable.pm
- http://code.sixapart.com/svn/movabletype/branches/mt4.21/lib/MT/Scorable.pm
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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.