Subject: | roles w/ common attributes treated differently when composed w/ create_class_with_roles vs direct composition and apply_roles_to_package |
Given these two essentially identical roles,
package R1 {
use Moo::Role;
has a => ( is => 'rw', builder => sub { {} } );
after _build_a => sub { say __PACKAGE__ };
}
package R2 {
use Moo::Role;
has a => ( is => 'rw', builder => sub { {} } );
after _build_a => sub { say __PACKAGE__ };
}
I would expect that the following three approaches to composition would be equivalent:
--------------
package C1 {
use Moo;
with 'R1';
with 'R2';
}
C1->new->a;
--------------
package C2 { use Moo; }
my $class = Moo::Role->create_class_with_roles( 'C2', 'R1' );
Moo::Role->apply_roles_to_package( $class, 'R2' );
$class->new->a;
--------------
package C3 { use Moo; }
Moo::Role->apply_roles_to_package( 'C3', 'R1' );
Moo::Role->apply_roles_to_package( 'C3', 'R2' );
C3->new->a;
However, they are not.
% perl test.pl
---------------
C1:
R1
R2
---------------
C2:
R2
---------------
C3:
R1
R2
The results for the first and last approaches are what I expect to happen. Something seems to be going awry with create_class_with_roles.
Attached is the example code.
Thanks!
Diab
Subject: | test.pl |
use 5.010;
use strictures 2;
use Moo::Role ();
package R1 {
use Moo::Role;
has a => ( is => 'rw', builder => sub { {} } );
after _build_a => sub { say __PACKAGE__ };
}
package R2 {
use Moo::Role;
has a => ( is => 'rw', builder => sub { {} } );
after _build_a => sub { say __PACKAGE__ };
}
package C1 {
use Moo;
with 'R1';
with 'R2';
}
say '---------------';
say 'C1:';
C1->new->a;
say '---------------';
say 'C2:';
package C2 { use Moo; }
my $class = Moo::Role->create_class_with_roles( 'C2', 'R1' );
Moo::Role->apply_roles_to_package( $class, 'R2' );
$class->new->a;
say '---------------';
say 'C3:';
package C3 { use Moo; }
Moo::Role->apply_roles_to_package( 'C3', 'R1' );
Moo::Role->apply_roles_to_package( 'C3', 'R2' );
C3->new->a;