develooper Front page | perl.moose | Postings from April 2009

Roles, Classes and silent overriding

Thread Next
From:
Stevan Little
Date:
April 14, 2009 11:23
Subject:
Roles, Classes and silent overriding
Message ID:
547D2377-8169-416C-B5AC-66BF247292CD@iinteractive.com
So, Ovid has a new Moose related post on his use.perl journal (http://use.perl.org/~Ovid/journal/38809 
) which sparked a discussion on #moose IRC channel.

The core of Ovid's issue is that when a role is composed into a class,  
the class wins in any method conflicts. This is how roles were  
originally specced in the traits paper and how they have always worked  
(at least every time I wrote them). However Ovid makes a good point  
that in overriding the role method "silently" it can make for some  
very hard to debugging.

So, while I am not willing to change this behavior, I am willing to  
add a warning so that it is not so "silent" anymore. So code like this  
will warn you that your overriding a class method.

   package Foo::Role;
   use Moose::Role;

   sub foo { ... }

   package Foo::Class;
   use Moose;

   with 'Foo::Role';

   sub foo { ... }

You will be able to silence this warning by explicitly excluding the  
method from the role, like so:

   package Foo::Role;
   use Moose::Role;

   sub foo { ... }

   package Foo::Class;
   use Moose;

   with 'Foo::Role' => { excludes => 'foo' }; # excludes => [ 'foo' ]  
also works if you have more then one method

   sub foo { ... }

This should not only help with debugging, but also in refactoring  
because it will be able to catch possibly unintended issues before  
they become problems. This change is 100% backwards compat, but if you  
don't want warnings you will have to be more explicit.

Any comments, questions, issues?

- Stevan

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