On Sun, Jan 09, 2011 at 10:53:47PM -0800, Ovid wrote:
> Per a suggestion from Stevan, I've taken the role tests from Moose and
> started
> applying them to Role::Basic. I came across a case I'm not familiar with. If
> a
> role consumes *one* other role and they share a method with the same name, a
> conflict results:
>
> package Some::Role;
> use Moose::Role;
> sub bar { __PACKAGE__ }
>
> package Some::Other::Role;
> use Moose::Role;
> with 'Some::Role';
> sub bar { __PACKAGE__ }
>
> package Some::Class;
> use Moose;
> with 'Some::Other::Role';
>
> package main;
> my $o = Some::Class->new;
> print $o->bar; # Output: Due to a method name conflict in roles
> 'Some::Other::Role' and 'Some::Role', the method 'bar' must be implemented or
> excluded by 'Some::Class'
>
> However, if a role consumes *two or more* other roles and they share a method
> with the same name, the consumed role's methods are silently excluded:
>
> package Some::Role;
> use Moose::Role;
> sub bar { __PACKAGE__ }
>
> package Some::Other::Role;
> use Moose::Role;
> sub bar { __PACKAGE__ }
>
> package Another::Role;
> use Moose::Role;
> with qw(Some::Role Some::Other::Role);
> sub bar { __PACKAGE__ }
>
> package Some::Class;
> use Moose;
> with 'Another::Role';
>
> package main;
> my $o = Some::Class->new;
> print $o->bar;
> # Output: Another::Role
>
> This is apparently deliberate behavior as it's explicitly tested for
> in t/030_roles/011_overriding.t:
>
> # check that when two roles are composed, they conflict
> # but the composing role can resolve that conflict
>
> Is there a set of rules somewhere explaining what are conflicts and what are
> not
> (and why)?
http://search.cpan.org/dist/Moose-1.21/lib/Moose/Spec/Role.pod
In particular, the "Composition Edge Cases" section covers this issue.
-doy