Questions tagged [mixins]

18 questions
66
votes
1 answer

How are mixins or traits better than plain multiple inheritance?

C++ has plain multiple inheritance, many language designs forbid it as dangerous. But some languages like Ruby and PHP use strange syntax to do the same thing and call it mixins or traits. I heard many times that mixins/traits are harder to abuse…
Gherman
  • 945
  • 1
  • 7
  • 13
54
votes
5 answers

Are Python mixins an anti-pattern?

I'm fully aware that pylint and other static analysis tools are not all-knowing, and sometimes their advice must be disobeyed. (This applies for various classes of messages, not just conventions.) If I have classes like class related_methods(): …
cat
  • 734
  • 1
  • 7
  • 15
20
votes
4 answers

Inheritance vs mixins in dynamic languages?

When should you prefer inheritance patterns over mixins in dynamic languages? By mixins, I mean actual proper mixing in, as in inserting functions and data members into an object in runtime. When would you use, for example, prototypal inheritance…
Magnus Wolffelt
  • 2,373
  • 2
  • 20
  • 23
19
votes
1 answer

Why not make a language with mixin-only inheritance?

It seems that in all class-based or prototypal OOP languages, mixins are either an afterthought or a secondary feature. However, to me it looks like traditional inheritance is just a specific case of using a single mixin. I am assuming this is…
Den
  • 4,827
  • 2
  • 32
  • 48
13
votes
4 answers

Parallel hierarchies - partly same, partly different

There are quite a few similar questions out there 1, 2, 3, 4, but non seems exactly the case in this question, nor do the solutions seem optimal. This is a general OOP question, assuming polymorphism, generics, and mixins are available. The actual…
10
votes
2 answers

What's the difference between a Mixin and a Trait?

From what I can tell from Scala and Hack- Mixins: Can have state (ie. instance properties) Can only provide concrete methods Can have constructors, that are called in the same order that their classes were mixed in If A mixes in B and C, A…
bcherny
  • 253
  • 2
  • 6
8
votes
2 answers

Are Interfaces with Java 8 Virtual Extension Methods the Same Thing as Mixins?

This post describes a new feature in Java 8 called virtual extension methods (formerly called default methods, or defender methods). In the example provided, an interface has one method, which is implemented by a class. Then a second method is…
Thunderforge
  • 2,668
  • 3
  • 23
  • 30
6
votes
1 answer

Why would it be considered bad practice if a concrete class inherits from only one Mixin?

I'm referring to "Fluent Python" by Luciano Ramalho. In chapter 12, there's a section "Coping with Multiple Inheritance" where the author suggests a few best practices, such as: Distinguish Interface Inheritance from Implementation Inheritance Make…
Lagerbaer
  • 526
  • 3
  • 9
6
votes
0 answers

Relationship between a mixin and its master invokation

I haven't studied Smalltalk or Strongtalk and am just trying to get an overview of the semantics of interfaces and polymorphism in O/O languages, particularly Dart. In the 2002 paper titled Mixins in Strongtalk, there is the following paragraph in…
Tom Russell
  • 161
  • 3
5
votes
1 answer

JavaScript extend vs mixin

After having read Eric Elliott's Fluent JavaScript article, I was and still am toughtful about the way to play with instance prototypes. On one side, you have the extending inheritance... var B = function() {} ; B.prototype = new A() ; Or the new…
Tot
  • 147
  • 1
  • 6
5
votes
1 answer

Does a Mixin By Definition Allow Storing Data in Mixin-Level Instance Variables?

Several implementations of mixins (e.g. Ruby mixin, Scala trait) include support for storing data (e.g. variables, properties) between method calls in instance variables. There was a bit of debate in this question about whether or not a Java virtual…
Thunderforge
  • 2,668
  • 3
  • 23
  • 30
5
votes
2 answers

Python multiple inheritance or decorators for composable behaviours

I recently discovered (or rather realised how to use) Python's multiple inheritance, and am afraid I'm now using it in cases where it's not a good fit. I want to have some starting data source (NewsCacheDB,TwitterStream) that gets transformed in…
Matti Lyra
  • 153
  • 1
  • 5
2
votes
2 answers

Is a god class still bad practice if it is used with mixins?

Every description I have read about god classes considers them to be an anti-pattern and a bad practice. Most descriptions I have read about mixins considers them to be acceptable in some cases. If the functionality of a god class is broken up into…
J. Darnell
  • 147
  • 4
2
votes
1 answer

When to use mixins in Ruby

I am wondering when to use mixins? I have read about them. Many authors compare them to interfaces, abstract classes, etc. Mixins are modules that are mixed-in and modules are a way to group similar methods, constants and classes together. I have…
Gilles
  • 2,201
  • 1
  • 19
  • 26
1
vote
1 answer

Ruby: Abusing mixin

I'm currently working with some code that IMO has abused ruby's mixin features. Given that I'm new to ruby, I wonder if MO in IMO is correct or not. My primary question is what makes more sense (and if there are any major technical differences…
1
2