The incredible return of mixin.pm!

schwern on 2007-03-20T03:53:04

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.


SOTA

chromatic on 2007-03-20T06:14:01

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 ++

mixins versus traits

Ovid on 2007-03-20T09:05:01

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.

mixing in with SEx

rjbs on 2007-03-20T13:17:12

I do most of my mixing in of methods with Sub::Exporter.

I end up not often needing the override-and-super feature that I see as being the real benefit of mixin.pm -- but Sub::Exporter::Util::mixin_exporter can do that if I need it.

C3 heirarchy conversion woes

blblack on 2007-03-21T22:00:02

Assuming that your diamondy inheritance heirarchy is somewhat sane, the most common problems that cause a hierarchy to be invalid are re-ordering of dependencies, which is relatively easy to spot and fix.

Example:

package A; our @ISA = qw/B C/; package B; our @ISA = qw/D E/; package C; our @ISA = qw/E D/;

Because A inherits from B+C, and they each inherit D+E in a different order, C3 cannot resolve this. The fix is to use D+E in the same order in both places.

Most issues converting legacy hierarchies to C3 basically boil down to some (possibly very complex) variant of this sort of thing, in either the horizontal or vertical direction. I've come to think that if a hierarchy doesn't pass C3 muster, it was a bit psycho to begin with :)

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.