tima has asked for the wisdom of the Perl Monks concerning the following question:
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.