einhverfr has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks

I have a moose class with has an attribute which is an arrayref of objects. I would like to require that each object consumes a specific role but I don't want to require an isa relationship to a specific class.

Consider

package myclass; use moose; # this won't work since ElementRole is a role has parts => (is => 'ro', isa => 'ArrayRef[ElementRole]'); # this won't work since it is an arrayref has parts => (is => 'ro', does => 'ElementRole');

Is there a way to do this or should I just revert to an abstract superclass?

Replies are listed 'Best First'.
Re: Moose, arrayref, and does?
by tobyink (Canon) on Oct 06, 2014 at 13:53 UTC
Re: Moose, arrayref, and does?
by McA (Priest) on Oct 06, 2014 at 13:51 UTC

    Hi,

    have a look at ConsumerOf.

    UPDATE: While searching for the right reference, Tobyink posted a more complete and definitively more authorative answer. Therefore, look below.

    Regards
    McA

Re: Moose, arrayref, and does?
by tobyink (Canon) on Oct 07, 2014 at 18:53 UTC

    Here's another answer...

    use MooseX::Types::MoreUtils; isa => "ArrayRef"->$_of({ role => "My::Role" })