Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

required is not applied with role consuming in run-time #88

Open
potatogim opened this issue Jul 27, 2018 · 5 comments
Open

required is not applied with role consuming in run-time #88

potatogim opened this issue Jul 27, 2018 · 5 comments

Comments

@potatogim
Copy link

potatogim commented Jul 27, 2018

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 potatogim changed the title required is not applied with consuming role in run-time required is not applied with consuming role in run-time Jul 27, 2018
@potatogim potatogim changed the title required is not applied with consuming role in run-time required is not applied with role consuming in run-time Jul 27, 2018
@sergeykolychev
Copy link
Contributor

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

@potatogim
Copy link
Author

potatogim commented Jul 28, 2018

@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

@sergeykolychev
Copy link
Contributor

@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 added a commit to sergeykolychev/p5-Mouse that referenced this issue Jul 29, 2018
Making sure that applying a role in runtime to an instance properly checks for the existence of the role's required attributes in that instance.
@sergeykolychev
Copy link
Contributor

Hi @potatogim,
I coded a fix, #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.

@potatogim
Copy link
Author

potatogim commented Jul 30, 2018

@sergeykolychev : It is the latter :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants