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)?
Cheers,
Ovid--
Live and work overseas - http://overseas-exile.blogspot.com/
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://blogs.perl.org/users/ovid/
Twitter - http://twitter.com/OvidPerl/