171

What is the difference between Mixins and Traits?

According to Wikipedia, Ruby Modules are sort of like traits. How so?

mghie
  • 32,028
  • 6
  • 87
  • 129
KaptajnKold
  • 10,638
  • 10
  • 41
  • 56

2 Answers2

235
  1. Mixins may contain state, (traditional) traits don't.
  2. Mixins use "implicit conflict resolution", traits use "explicit conflict resolution"
  3. Mixins depends on linearization, traits are flattened.

Lecture about traits

ad 1. In mixins you can define instance variables. Traits do not allow this. The state must be provided by the composing class (=class using the traits)

ad 2. There may be the name conflict. Two mixins (MA and MB) or traits (TA and TB) define the method with the same definition foo():void.

Mixin MA {
    foo():void {
        print 'hello'
    }
}

Mixin MB {
    foo():void {
        print 'bye'
    }
}

Trait TA {
    foo():void {
        print 'hello'
    }
}

Trait TB {
    foo():void {
        print 'bye'
    }
}

In mixins the conflicts in composing class C mixins MA, MB are resolved implicitly.

Class C mixins MA, MB {
    bar():void {
        foo();
    }
}

This will call foo():void from MA

On the other hand while using Traits, composing class has to resolve conflicts.

Class C mixins TA, TB {
    bar():void {
        foo();
    }
}

This code will raise conflict (two definitions of foo():void).

ad 3. The semantics of a method does not depend of whether it is defined in a trait or in a class that uses the trait.

In other words, it does not matter whether the class consists of the Traits or the Traits code is "copy - pasted" into the class.

cngzz1
  • 143
  • 2
  • 9
jk_
  • 5,448
  • 5
  • 25
  • 23
  • 6
    I know it's a year past date, but for future readers, in ruby it would use the method form the last module that was mixed in, so it would call foo() form MB – rik.vanmechelen Jan 12 '12 at 19:36
  • 4
    in Scala traits can have fields, this imply they are not "traditional" traits ? – Sergio Sep 20 '12 at 16:31
  • 4
    Yes, these are not "traditional", they are called "statefull" traits. Difference between statefull traits and mixins are points 2 and 3. – jk_ Sep 24 '12 at 14:49
  • 2
    What do you mean with linearization? – Aykut Kllic Sep 16 '13 at 15:52
  • afaik Scala traits do depend on linearization. – Sergio Nov 02 '13 at 20:56
  • 10
    Tentative -1; the way the terms 'trait' and 'mixin' are used in the wild is highly inconsistent and at least one of the points here is mostly wrong. PHP and Wikipedia (and according to @Sergio, also Scala) disagree with you about traits being stateless. I don't find this answer useful because it's made up of bare assertions, and it's unclear to me that this is anything more than how you personally use these words. To be convinced otherwise, I'd need to see many examples of how the terms are used in the real world (e.g. in real programming languages) to back up your claims. – Mark Amery Mar 13 '14 at 14:59
  • 3
    @AykutKllic Linearization -> "The compiler resolves the question of what super is, without ambiguity." (http://www.ibm.com/developerworks/library/j-jn8/) – ben Oct 24 '14 at 02:31
  • 4
    Would you kindly elaborate on the *"Mixins depends on linearization, traits are flattened"* part? I'm not sure whether i understand it. – Adam Arold Jul 31 '15 at 11:42
  • It's not true that method inheritance conflict resolution with traits have to be handled explicitly. In PHP traits are handled implicitly. – Sebi2020 Sep 19 '18 at 23:22
  • @Sebi2020 That's not right. PHP throws a fatal error `Trait method X has not been applied, because there are collisions with other trait methods`. – revo Feb 13 '20 at 20:35
  • was this answer specific to one language or generally applied to distinct (if there's any distinctions) between the two term? – dragonxlwang Jan 08 '21 at 22:03
9

These pages explain the difference in the D Programming language.

http://dlang.org/mixin.html

http://dlang.org/traits.html

Mixins in this context are code generated on the fly, and then inserted at that point in code during compilation. Quite handy for simple DSLs.

Traits are compile-time external values (rather than code generated from an external source). The difference is subtle. Mixins add logic, Traits add data such as compile-time type information.

Don't know much about Ruby, but hope this helps somewhat.

fooquency
  • 1,575
  • 3
  • 16
  • 29
Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
  • 48
    Mixins and Traits in D are completely different from what the terms mean in Computer Science generally. In D, both are preprocessor primitives for automatic code generation. In other languages, they are inheritance mechanisms. The naming decision in D is unfortunate. – tylerl Feb 24 '12 at 17:36