p5-Mouse icon indicating copy to clipboard operation
p5-Mouse copied to clipboard

`required` is not applied with role consuming in run-time

Open potatogim opened this issue 6 years ago • 5 comments

Hello!

I'm trying to consume a role that has an attribute is specified required in run-time.

In Moose, it works gracefully but Mouse does not throw exception even though required attribute is not specified in constructor(MyPackage->new(...)).

potatogim avatar Jul 27 '18 06:07 potatogim

@potatogim I am having problems to reproduce. Can you please add minimal example ?

sergeykolychev avatar Jul 28 '18 08:07 sergeykolychev

@sergeykolychev : Sure.

package MyRole;

use Moose::Role;

has 'name' =>
(
    is       => 'ro',
    isa      => 'Str',
    required => 1
);

no Moose::Role;

package MyClass;

use Moose;
use Moose::Util;

sub BUILD
{
    my $self = shift;
    my $args = shift;

    Moose::Util::apply_all_roles($self, 'MyRole');
}

__PACKAGE__->meta->make_immutable;

package main;

my $obj = MyClass->new();

1;

It throws an exception like below

Attribute (name) is required at /home/potatogim/.perl5/perlbrew/libs/perl-5.26.1@mop/lib/perl5/x86_64-linux-thread-multi/Moose/Util.pm line 113
	Moose::Util::apply_all_roles('Moose::Meta::Class::__ANON__::SERIAL::1=HASH(0x55616408ff98)', 'MyRole') called at test.pl line 26
	MyClass::BUILD('Moose::Meta::Class::__ANON__::SERIAL::1=HASH(0x55616408ff98)', 'HASH(0x5561630e81e0)') called at constructor MyClass::new (defined at test.pl line 29) line 28
	MyClass::new('MyClass') called at test.pl line 33

but, If change Moose to Mouse

perl test.pl
Use of uninitialized value in printf at test.pl line 35.
name: 

Even though we can use Meta-constructor or MouseX::Traits like below(AFAIK, overriding new() is not good way), we may need to consider this situation IMHO :)

package MyRole;

use Mouse::Role;

has 'name' =>
(
    is       => 'ro',
    isa      => 'Str',
    required => 1
);

no Mouse::Role;

package MyClass;

use Mouse;

# Role consuming
with 'MouseX::Traits';

around 'new' => sub
{
    my $orig = shift;
    my $self = shift;

    $self->with_traits('MyRole')->$orig(@_);
};

__PACKAGE__->meta->make_immutable(inline_constructor => 0);

package main;

my $obj = MyClass->new();

printf "name: %s\n", $obj->name;

1;
Attribute (name) is required at test.pl line 26.
	MyClass::__ANON__(CODE(0x5575a6d87568), "MyClass") called at /home/potatogim/.perl5/perlbrew/libs/perl-5.26.1@mop/lib/perl5/x86_64-linux-thread-multi/Mouse/Meta/Class.pm line 391
	Mouse::Meta::Class::__ANON__("MyClass") called at /home/potatogim/.perl5/perlbrew/libs/perl-5.26.1@mop/lib/perl5/x86_64-linux-thread-multi/Mouse/Meta/Class.pm line 345
	MyClass::new("MyClass") called at test.pl line 33

potatogim avatar Jul 28 '18 16:07 potatogim

@potatogim pretty interesting usage, do you just want full parity with Moose in this case or you have a need in your application to apply roles on objects ?

sergeykolychev avatar Jul 29 '18 03:07 sergeykolychev

Hi @potatogim, I coded a fix, https://github.com/gfx/p5-Mouse/pull/89 but not really sure if the issue warrants to be fixed and important. If the maintainers decide that it's important then probably it'll be merged, we'll see. Thanks.

sergeykolychev avatar Jul 29 '18 07:07 sergeykolychev

@sergeykolychev : It is the latter :)

potatogim avatar Jul 30 '18 00:07 potatogim