Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Object::InsideOut and Roles/Mixins

by Yary (Scribe)
on Apr 11, 2014 at 17:43 UTC ( #1082004=note: print w/ replies, xml ) Need Help??


in reply to Re: Object::InsideOut and Roles/Mixins
in thread Object::InsideOut and Roles/Mixins

Thanks... I would also want O::IO "fields" (what Moose calls "attributes") in the roles, so will look at the other modules Role::Tiny refers to, Role::Basic and Moo::Role. I'll have to think more about fields and how these role-helpers work to pick the right one.

edit now I am in danger of writing Object::InsideOut::Role - not that I have the time to- but after re-reading about them, and comments implementers have made with the benefit of hindsight, and thinking about how I've already used and misused roles, it's tempting to roll something up taking it all into account.


Comment on Re^2: Object::InsideOut and Roles/Mixins
Re^3: Object::InsideOut and Roles/Mixins
by tobyink (Abbot) on Apr 11, 2014 at 21:22 UTC

    It's difficult to get roles and attributes to play nice. Attributes basically have two parts to the implementation:

    1. They need to be initialized in the constructor; and

    2. You need to provide accessors for them.

    The accessors are easy. But your role framework doesn't have any control over the constructor; the constructor gets built by the class framework!

    That's why if you look at Perl implementations of roles, you'll see them fall into two neat categories:

    • Moose::Role, Mouse::Role, Moo::Role, and p5-mop roles all allow you to define attributes in roles.

      These role implementations are all part of larger frameworks that include a class builder.

    • Role::Tiny and Role::Tiny do not allow you to define attributes in roles.

      These role implementations are stand-alone.

    So basically, if you want to write a role implementation that supports attributes, you'll need to write a class implementation to support it.

    I'm not saying you shouldn't do it. It's a good learning experience.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Thanks for that perspective. Here's another thought along those lines- at the minimum a role requires the composing class to implement some behavior (method). Maybe the role also has an implementation defined, which the class can just use. In that case, the role might want to have some data, or state, along with it, and then it's convenient for the role to also add an attribute- to support the method defined in that role.

      And that's where you need to mesh with the larger framework. Or roll your own per-object attribute/state variable, but isn't it better to use what your framework already provides?

      Then again, lack of attributes is not that big a deal. There's always package lexical hashes, or closures; also, getters and setters are just methods. And Role::Basic allows importing methods from some other class with that express purpose.

      TMTOWTDI!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-12-21 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls