Subject: | Moo::Role->apply_roles_to_object doesn't properly handle consumed attribute defaults |
Explicitly applying a role to an object doesn't construct the default value for a consumed attribute.
Here's some code:
----------------------------------------------------
use latest;
use Moo::Role '';
{
package Role;
use Moo::Role;
has a => ( is => 'ro', default => sub { 'default' } );
}
{
package A;
use Moo;
with 'Role';
}
{ package B;
use Moo;
has b => ( is => 'ro' );
}
my $a = A->new;
say $a->a;
my $b = B->new;
Moo::Role->apply_roles_to_object( $b, 'Role' );
say $b->a;
----------------------------------------------------
I expected it to output
------------------------
default
default
------------------------
Instead, I got
------------------------
default
Use of uninitialized value in say at test.pl line 35.
------------------------
Am I expecting too much? I'm expecting 'apply_roles_to_object' to be functionally equivalent to 'with'.
Thanks,
Diab
Here's some code:
----------------------------------------------------
use latest;
use Moo::Role '';
{
package Role;
use Moo::Role;
has a => ( is => 'ro', default => sub { 'default' } );
}
{
package A;
use Moo;
with 'Role';
}
{ package B;
use Moo;
has b => ( is => 'ro' );
}
my $a = A->new;
say $a->a;
my $b = B->new;
Moo::Role->apply_roles_to_object( $b, 'Role' );
say $b->a;
----------------------------------------------------
I expected it to output
------------------------
default
default
------------------------
Instead, I got
------------------------
default
Use of uninitialized value in say at test.pl line 35.
------------------------
Am I expecting too much? I'm expecting 'apply_roles_to_object' to be functionally equivalent to 'with'.
Thanks,
Diab