Subject: | attributes in a role override existing attributes |
My understanding is that when consuming a role neither existing methods nor attributes would be overridden.
However that doesn't seem to be the case for attributes. It seems that the last attribute loaded is the one which is implemented, rather than the first.
Attached are two tests; one using Moo, the other Moose. The Moose one passes, the Moo one doesn't.
Subject: | test-moo.pl |
#! perl
use Test::More;
{
package R1;
use Moo::Role;
has attr => ( is => 'ro', default => sub { __PACKAGE__ } );
sub meth { __PACKAGE__ }
}
{
package R2;
use Moo::Role;
has attr => ( is => 'ro', default => sub { __PACKAGE__ });
sub meth { __PACKAGE__ }
}
{
package A;
use Moo;
has attr => ( is => 'ro', default => sub { __PACKAGE__ });
with 'R1';
with 'R2';
}
is( A->new->attr, 'A', "don't override existing attribute in A" );
is( A->new->meth, 'R1', 'get method from R1' );
Subject: | test-moose.pl |
#! perl
use Test::More;
{
package R1;
use Moose::Role;
has attr => ( is => 'ro', default => sub { __PACKAGE__ } );
sub meth { __PACKAGE__ }
}
{
package R2;
use Moose::Role;
has attr => ( is => 'ro', default => sub { __PACKAGE__ });
sub meth { __PACKAGE__ }
}
{
package A;
use Moose;
has attr => ( is => 'ro', default => sub { __PACKAGE__ });
with 'R1';
with 'R2';
}
is( A->new->attr, 'A', "don't override existing attribute in A" );
is( A->new->meth, 'R1', 'get method from R1' );