3 ms·
That's a delegation strategy, which is a very limited form of composition. A class with a trait/role can be queried for that interface: smile() if $my_objec
by wschroed 12y ago
That's a delegation strategy, which is a very limited form of composition.
A class with a trait/role can be queried for that interface:
smile() if $my_object->does('SomeTrait');
Methods provided by a trait/role have direct access to $self.
Also, Moose gives you the ability to apply traits to classes that are not predefined with those traits:
use Moose::Util;
my $instance = Moose::Util::with_traits('MooseClass', 'MyTrait')->new(%params);
You can parameterize roles (as in, pass parameters that affect what the role provides). If you put some method modifiers into your roles, you can enhance existing functionality; for example, you can add logging, memoization, and persistence, all without the class having those features beforehand.
In other words, roles/traits make your OOP functionality much more composable.