develooper Front page | perl.moose | Postings from August 2016

Re: Why to use Moose roles ever?

Thread Previous | Thread Next
From:
Niall Young
Date:
August 11, 2016 03:43
Subject:
Re: Why to use Moose roles ever?
Message ID:
alpine.OSX.2.20.1608111339320.88165@niall.local
On Wed, 10 Aug 2016, Victor Porton wrote:

> Moose roles have some limitations, such as inability to override a
> method in a class which "with"es the role.
>
> But I can use an abstract base class instead of a role.
>
> Are there any real situations where roles are better than base classes?
> With examples, please.

I was going to suggest augment'ing a method in a Role, which I'd been planning to explore as a way of declaring interfaces and standard parameter/type checks in Classes, while consuming Roles for the implementation, but alas:

 	"Roles cannot support 'augment' at ..."

> This is false. For example:

Not sure how your example applies here Karen, there is no foo() declared in MyClass only a foo method modifier, and MyRole's foo() doesn't appear to be executed from your output?

>     package MyClass;
>     use Moose;
>     with 'MyRole';
>     around foo => sub {
>       'I came from the class';
>     };

> say $obj->foo;​ 
> ​# prints: I came from the class

--
Niall Young
niall@iinet.net.au
Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About