![]() |
|
Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Sanity Check: Roles vs. Traitsby stvn (Monsignor) |
on Oct 30, 2008 at 19:25 UTC ( #720552=note: print w/replies, xml ) | Need Help?? |
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. This "-able" thing is more aligned to how people use Java interfaces, which is only one way in which Roles/Traits can be used. Most of the time my Roles are named "HasDBICSchema" or something similar and simply provide, in this case, a DBIx::Class (commonly shortened to DBIC) Schema object. The reality is that Roles/Traits are still new, the smalltalk folks who invented traits are not seeing a lot of trait usage in the community (from my limited knowledge, so dont take that as 100% fact). The most usage is currently with Roles in the Moose community. Roles seem to be a more simple and basic implementation of the traits idea. Actually, its the opposite, roles can provide class attributes/slots which complicates them much more than traits which only allow methods. The deeper discussion of why this is so, is way out of the scope of this post, suffice to say that when I wrote Moose::Role I had a discussion with the original authors of the traits paper (whom I had already known from back when I had written Class::Trait), and when I asked them if they had figured out how to do slots in traits they said "No, but let us know if you figure it out". They have since written their own smalltalk version with attributes/slots and ended up having to add public/protected/private to Smalltalk and I got the impression they were generally unhappy with the result. Traits includes more advanced features like overloaded operators, required method labels, conflict resolution, aliases and summation. Actually, Moose::Role supports required methods, conflict resolution and aliasing. Summation and conflict detection are just how roles work, so I dont consider it a feature. Only Class::Trait supports overloaded operators, and the pain of having to write that is exactly why Moose::Role does not support overloaded operators. Roles will be built-in to Perl 6. Traits will not. Traits can really be viewed as a subset of Roles. Perl 6 doesnt support explicit method requirement, but simply doing method foo { ... } using the yadda-yadda-yadda operator will give you pretty much the same thing. As for conflict resolution, you can do that in a more manual way in Perl 6 then you can in Class::Trait or Moose::Role, but it is still generally possible. 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? None of the above, all those modules should be considered toy implementations and are all woefully incomplete. The only complete and thoroughly tested Role implementation on the CPAN is Moose::Role.
-stvn
In Section
Seekers of Perl Wisdom
|
|