Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

In Newspeak all classes are mixins. Here are some snippets from Gilad Bracha's answer to a similar question in Newspeak discussion forum:

Mixins are not a feature of Newspeak per se. That is, we did not design the language saying, ok, now we'll add mixins. Mixins fall out automatically from class nesting and message based semantics. That is, if you have virtual classes, you have mixins unless you actually ban them. ...

Traits attempt to address perceived problem of mixins.

  1. There is very little real experience indicating that these perceived problems are real.
  2. Traits are restricted to be stateless. This simplifies matters, but does not handle all cases of interest. In fact, there are now research papers attempting to add state to traits.

Traits are entirely subsumed by a more general model, which I devised many years ago in my PhD thesis (available off my web site, if you really want to dig deep). ... I would like to examine how we might incorporate these combinators into Newspeak. ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top