Normally I would finalize a class at compilation via __PACKAGE__->meta->make_immutable
at the end of the class. However, when should I be making a class immutable that composes roles into itself at runtime? Should I even be doing so to get better performance or is this imcompatible with make_immutable
? make_immutable
seems to speed up instantiation of the object, but does it do anything once the object is instantiated?
For example, something along the lines of:
BUILD {
my $self = shift;
use Module::Load;
### Use the arguments passed in to determine roles applicable to
### this instance of the object. Load and apply roles.
for my $role ($self->_determine_roles()) {
load $role;
$role->meta->apply($self);
}
### $self is now a Class::MOP::Class::__ANON__... anonymous class
### Should I then be saying I'm done mutating it with something like this?
### can make_immutable even be run on an object instance and not a package?
$self->meta->make_immutable;
}
Even if the code above works for a single package, what happens when a object reblesses itself with the 'Foo'
role, resulting in a anonymous class, then a second object blesses itself with a 'Foo'
(becoming that same anon class) then 'Bar'
role? Is it going to work correctly when the second object blesses itself into the immutable first anonymous class, then tries applying the role to the now-immutable anonymous class to create a new anonymous class?
From reading the docs on Moose::Meta::Class, it appears only a class can be immutable, not instances of objects. If so, should I just be ignoring make_immutable
as I am mutating my classes?