views:

326

answers:

3

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

+5  A: 

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.

Lukas Renggli
The fragility usually associated with multiple inheritance is "the diamond problem" - no deterministic resolution of method with same name and signature which is inherited from multiple classes. In case of mixins, as you already noted, the composition order determines how methods are resolved, so mixins don't suffer from the "diamond problem".
Yardena
Yes they do, but it's in a biased way. If you get two methods `m()` and `n()` from both `Mixin1` and `Mixin2`, then you can only have both methods from one mixin. If you want one method from `Mixin1` and the other from `Mixin2`, you're stuck.
Damien Pollet
What you describe is not what is known as "the diamond problem". I'm not sure if it's a problem at all - mixin is supposed to represent a minimal unit of reusable behavior, so naturally it can't be broken down any further...
Yardena
+5  A: 

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

Yardena
+3  A: 

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 (yes, this is plural), pearls don't depict the problem as well as rubies.

Jordão