On Wed, Mar 14, 2012 at 06:37:34PM +0000, Tomas Doran wrote: > > On 14 Mar 2012, at 17:21, Caleb Cushing wrote: > > I'm not saying new_with_traits is bad, or with_traits is bad, just > > that I'd like to be able optionally turn on a way to do it with just > > ->new . regardless of whether it changes peoples expectations of what > > new does (because otherwise I have to change a different expectation, > > which construction method to use ) > > package MyApp::Role::TraitsInBuild; > use Moose::Role; > > sub BUILD {} > after BUILD => sub { > my ($self, $args) = @_; > foreach (@{$args->{traits}}) { > $_->meta->apply($self); > } > };
Moose::Util::apply_all_roles($self, @{$args->{traits}}) is better, since it handles conflicts between methods in the roles. > Or you can around new => sub {… and have the logic there… > > Or whatever you like really. > > I don't think that what you want is at all hard, I just think it's a > bad idea to do it as a standard in any way.. :) Yes. There's no expectation that ->new is the only way to create new objects, classes have multiple constructors all the time (this is common in pretty much any object oriented language, not just in Perl). These other constructors are typically just wrappers around the default constructor, but this is yet another reason to not shove all of this logic into the default constructor - it makes adding additional ways to create instances of your class even harder. -doy