|What if I want to mixin with anything?||Sometimes a mixin does not care what it mixes in with. Consider a logging or error handling mixin. For these, simply mixin with UNIVERSAL.|
|Why do I have to declare what I mixin with?||
Two reasons. One is technical, it allows SUPER to work.
The other is organizational. It is rare that a mixin is intended to be mixed with any old class. It often uses methods as if it were a subclass. For this reason it is good that it declares this relationship explicitly else the mixee wont be aware of the mixins expectations.
|Why use mixins instead of traits?||
Good question. Traits are definately a better idea then mixins, but mixins have two advantages. Theyre simpler to explain, acting like a gateway drug to traits by introducing the concept of OO reuse by class composition rather than inheritance.
The other is mixins work more like a drop-in replacement for multiple inheritance. In a large, hairy hierarchy mixins can often be used to trim the inheritance bush and make sense of things with a minimum of modification to the code. Once this basic repair is done, the work of converting to traits can begin.
If these advantages dont apply, proceed directly to traits.
Michael G Schwern <email@example.com>
Copyright 2002-2010 by Michael G Schwern
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
mixin, ruby from which I stole this idea.
|perl v5.20.3||MIXIN::WITH (3)||2016-03-17|