Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Sanity Check: Roles vs. Traits

by stvn (Monsignor)
on Oct 30, 2008 at 19:25 UTC ( [id://720552]=note: print w/replies, xml ) Need Help??


in reply to Sanity Check: Roles vs. Traits

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

Replies are listed 'Best First'.
Re^2: Sanity Check: Roles vs. Traits
by tima (Acolyte) on Oct 30, 2008 at 23:07 UTC

    Thanks for the through reply. I have to admit that I feel more confused when I started though.

    Looking at the various implementations of roles and traits it seemed the other way around in complexity based on their APIs.

    I'm interested in these sort of OOP concepts, but for the moment I'm just going to focus on how this sort of concept would apply to MT as it exists today and in the foreseeable future.

    > This "-able" thing is more aligned to how people use Java interfaces, which is only one way in which Roles/Traits can be used.

    So this -able concept could (should?) be a role or trait even if its not using the full spectrum of possibilities right now.

    > None one 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.

    I don't see being incomplete as an entirely bad thing. MT now makes use of YAML at ever increasing levels, but only uses YAML::Tiny for the processing. Given the complexity of the app and its installation and its prerequisites I see anything that can simplify and slim down the system as a great thing.

    So while these implementations are "toys" how is their upwards compatibility with the full roles vision? If one of these classes provides enough of the functionality MT needs now, like YAML::TIny is to YAML, I don't see what it would hurt to use it.

    Based on how I laid things out would do you think Roles in, even in the limited submit that these modules present, is better then the current multiple inheritance scheme MT is using thus far?

      Looking at the various implementations of roles and traits it seemed the other way around in complexity based on their APIs.

      This is because they are mostly experiments/toys and so there was no serious attempt to make compatible APIs between them (Perl6::Role actually states that compat is not one of it's goals). I really do not suggest using any of them, not because I wrote another implementation but because I studied all of them and spoke to their authors about them and they are not really maintained anymore.

      • Class::Role is untouched for 5 years, written by Luke Palmer, who is a really good programmer but I know (because Luke told me) that this module was just an experiment to try out the ideas from the original paper.
      • Class::Roles is also untouched for 5 years, this one written by chromatic and I suspect also an experiment to prove the usefulness of ideas in original Traits paper since chromatic was one of the people who brought the paper to the attention of the Perl6 group originally.
      • Perl6::Role is untouched for 3 years, written mostly by dragonchild when he and I worked together, it was put together to help Tim Bunce in the prototyping of DBI version 2, as far as I know dragonchild has not used this module much and last I knew DBI v2 was using Moose.
      So this -able concept could (should?) be a role or trait even if its not using the full spectrum of possibilities right now.

      Sure, there is no need to use all the features of the role, a simple role as interface is very useful too.

      I don't see being incomplete as an entirely bad thing. MT now makes use of YAML at ever increasing levels, but only uses YAML::Tiny for the processing. Given the complexity of the app and its installation and its prerequisites I see anything that can simplify and slim down the system as a great thing.

      So while these implementations are "toys" how is their upwards compatibility with the full roles vision? If one of these classes provides enough of the functionality MT needs now, like YAML::TIny is to YAML, I don't see what it would hurt to use

      In this case it is, these modules are not like YAML::Tiny is to YAML where it is a well tested and maintained subset of the functionality of a larger module. These are simply early experiments of the concept that are no longer maintained.

      If you are looking for a "Tiny" Moose then you can take a look at Mouse which does not have complete role support yet, but is being actively developed and maintained and will be downward compatible with Moose roles.

      Based on how I laid things out would do you think Roles in, even in the limited submit that these modules present, is better then the current multiple inheritance scheme MT is using thus far?

      I think that in most cases Roles are far superior to multiple inheritance in that they do compile time conflict checking and are composed in an unordered way and so tend to be more predictable. However, they are a new concept with not too much written about them and do have a bit of a learning curve (people tend expect them to work like classes/mixins which is not how they work).

      -stvn
        I think that in most cases Roles are far superior to multiple inheritance in that they do compile time conflict checking and are composed in an unordered way and so tend to be more predictable.

        So would you say it's better to try to use Roles exclusively, or a mix of inheritance and roles?

        Recently I've tried to not use inheritance for implementation convenience, only when it's semantically "correct", i.e. when A really is-a B.

        In my experience, using both inheritance and roles makes for a more complicated (and possibly confusing) code structure. You need to remember which things are roles a and which things are base classes. At one point, it turned out we needed to create a base class to extract common configuration, and the proper name for that base class would be the same name as an existing role.

        I think I've just recently passed the phase where I consider Roles to be "cool". You know, like when newbies encounter the GoF book and suddenly everything looks like a pattern.

        So while Roles are a great tool to use, I think we (as a community) need more experience using them, more guides on when and how to use them, and how to name roles (vs naming classes).

        /J

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://720552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 04:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found