I want to define a role that will use a type defined by its consumer.

Is this possible, and if so, what is the syntax to code it?

Here's an example of what I'm trying to achieve:

package Generic::Parent;

use Moose::Role;

require 'CHILD_CLASS';

has 'children' => (
    is => 'ro',
    isa => "HASHREF[$CHILD_CLASS]",
);

...

package Generic::Child;

use Moose::Role;
...

package X:Child;

use Moose;
with Generic::Child;

...


package X::Parent;

use Moose;
use X::Child;

subtype CHILD_CLASS as 'Object[X::Child]';

with Generic::Parent;


So, the X::Parent would consume the Generic::Parent role, which would use 
HashRef[X::Child] as the type for its children attribute; but elsewhere in the 
same program, there could be a Y::Parent that consumes the same Generic::Parent 
role but uses HashRef[Y::Child] for the type of its children attribute.

The only obvious way I can see, is to simply have the Generic::Parent require 
'children' and have each specific Parent consumer have to define the attribute 
themselves (and trust them to all consistently provide a hash of Child objects 
correctly).  However, that gets very awkward when the role wants to have a 
bunch of places that should be validating that a child object is consistent 
with the class of parent object that is consuming the role.  If the consumer 
has to know all of the internal details of the role and provide the roles data 
structures for it, that ruins the value of abstracting out the functionality of 
the role; so I hope there is a better way.


John Macdonald
Software Engineer

Ontario Institute for Cancer Research
MaRS Centre

661 University Avenue

Suite 510
Toronto, Ontario

Canada M5G 0A3


Tel:

Email: john.macdon...@oicr.on.ca

Toll-free: 1-866-678-6427
Twitter: @OICR_news


www.oicr.on.ca<http://www.oicr.on.ca/>

This message and any attachments may contain confidential and/or privileged 
information for the sole use of the intended recipient. Any review or 
distribution by anyone other than the person for whom it was originally 
intended is strictly prohibited. If you have received this message in error, 
please contact the sender and delete all copies. Opinions, conclusions or other 
information contained in this message may not be that of the organization.

Reply via email to