Re: Overriding additional attribute options with has +$foo
[email protected] (Stevan Little)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
Tom, On Apr 17, 2008, at 12:50 PM, Tomas Doran wrote: > > On 17 Apr 2008, at 16:34, Stevan Little wrote: >> Tom, >> >> Patch looks good, I will apply it this weekend likely. >> >> As for the traits support, it should be as simple as adding >> 'traits' to the list same as you did with 'metaclass'. > > I don't think that 'traits' should be supported in the same way as > metaclass however.. > > I think that if you have: > > has foo => ( > traits => [qw[SomeTrait]] > ); > > and in a derived package: > > has '+foo' => ( > traits => [qw[AnotherTrait]] > ); > > You should end up with: > > has foo => ( > traits => [qw[SomeTrait AnotherTrait]] > ); > > Which is what the docs / test suite in that patch is written for, > but will not (I believe) work by just adding to the list with > metaclass (instead you'd get replacement, and so have *only* > AnotherTrait).. > Please correct me if I'm wrong, or if my thoughts (that we should > combine, rather than override traits) are incorrect. Well, I see a few issues with trying to combine them. To start with the metaclass you are inheriting through +foo already has the SomeTrait applied, so if you combine them you are simply re- applying SomeTrait. Which is kind of silly, and unwrapping SomeTrait would be a giant pain and not likely worth it. Next, if you combine SomeTrait and AnotherTrait at the same time they will be subject to the role composition rules and so may conflict. Since SomeTrait was specified in the superclass, it could be argued that knowing of it's existence enough to be able to judge role conflict would be breaking the black-box-ness of the superclass. Of course black-box-ness of superclasses is overrated and not always possible in the real world. And if SomeTrait is way up the inheritance chain, then this could get annoying having to track it down. By not combining them and allowing them to override, you can bypass the role conflict stuff, and avoid needing to look up the inheritance chain. Also in 90% of the use cases the combination and the override will end up producing a class which is pretty much identical anyway, since in order to write good traits you need to write them in such a way that they are inherently combine-able. - Stevan