So Squeak/Pharo support Traits and Newspeak has Mixins. What is the difference? Traits have no instVars but Mixins have?
For a good comparison and for the reasoning of why traits are preferred, you can check the traits paper (pdf).
In essence, it's what Lukas Renggli said:
Traits members are composed into a class, and don't change its inheritance hierarchy. Conflicts have to be explicitly resolved by the user of the traits.
Mixins are linearized into the target class' inheritance hierarchy. If there're conflicting members, the order in which they were declared dictates which member gets called. This is fragile because it implicitly defines the behavior of the composition, and the class author must be aware of potential conflicts and how they'll impact the resulting class.
Since mixins get linearized, they don't suffer from the notorious "diamond problem" of multiple-inheritance. So the fragile nature in which they are stacked is another problem, which I'll dub the "ruby problem", to keep with the precious stone metaphor. For some odd reasons that have to do with moose, pearls don't depict the problem as well as rubies.
Traits are composed using a composition rule. Conflicts have to be resolved manually, it cannot happen that a trait accidentally overrides another method with the same name.
Mixins are composed by order and thus have fragility problems similar to multiple inheritance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With