Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a Squeak/Pharo Trait and a Newspeak Mixin?

So Squeak/Pharo support Traits and Newspeak has Mixins. What is the difference? Traits have no instVars but Mixins have?

like image 458
Richard Durr Avatar asked Feb 24 '10 21:02

Richard Durr


2 Answers

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.

like image 182
Jordão Avatar answered Sep 19 '22 20:09

Jordão


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.

like image 41
Lukas Renggli Avatar answered Sep 20 '22 20:09

Lukas Renggli