I just released mixin.pm 0.05, the first release in, ohhh, five years. I remember coming back from JOIN 2001 after having taken Dave and Andy's "Ruby in a Day" tutorial being all fired up about mixin instead of multiple-inheritance and eventually wrote mixin.pm and then promptly never used it. Its since languished on CPAN with but one lone bug in the queue.
Fast forward five years. I'm trying to convert a large application from Class::DBI to DBIx::Class. CDBI uses traditional inheritance. DBIx::Class uses the C3 method resolution algorithm which I'm not going to try and explain and don't fully understand myself but people say its better and fixes the diamond inheritance problem. Well... "fix" in the sense of "makes not work". When faced with some of the tangled hierarchies in this app it just throws its hands up and dies.
Some of the tangle comes from the fact that in several places an error handling module is inherited from. Boy it would be nice if they didn't have to inherit from it and could instead, somehow... stir its methods in. Whisk. Blend. Puree. Dare I say, mix?
I looked around on CPAN for what the state of the art in mixing is and after a cursory search didn't find anything that wasn't a full blown framework or even more alpha than my own mixin.pm. So I dusted it off and made a quick release to fix that one lone bug.
Enjoy.
I looked around on CPAN for what the state of the art in mixing is...
The state of the art of mixins in Perl 5 is Class::Trait.
Re:SOTA
Aristotle on 2007-03-20T06:18:24
Class::Trait ++
So you want to mixin your bomb class and your girlfriend class to your current class:
package Bomb;
sub fuse {... }
sub explode {... }
package Girlfriend;
sub fuse {... }
sub explode {... }
What happens when you want the Bomb::fuse method (because you can control the timing) but you also want the Girlfriend::explode method (because it's presumably non-lethal)? Now you have an ordering problem and you can't eliminate it. Of course, you could just use delegation, but that doesn't make life simple and that's part of what mixins are supposed to do. Traits allow you to solve this
But what if you already have a fuse or explode method defined in your code? mixins overwrite that. traits don't. Further, the traits won't even compile if there are conflicts unless you explicitly tell them how to resolve the conflicts.
Does another class want to know if you've implemented the Bomb methods? mixins don't really solve that (if you just use Girlfriend, it might have the false cognate problem, it might not). Traits, on the other hand, do solve that.
Need to conditionally bring those methods in at runtime or apply them to individual instances? Traits will do that, too.
Re:C3 heirarchy conversion woes
schwern on 2007-03-22T04:00:55
"Assuming that your diamondy inheritance heirarchy is somewhat sane"
I think I see the problem.