Questions tagged [mixins]

A mixin is a way to enhance an object with properties or methods from another object without inheriting from that object.

Mixins are related to inheritance in that an object receives properties or methods from another object but are typically not limited in number. They are often used as a way to "tack on" behavior to objects rather then to say that objects are related to each other.

For example, we may have an Album class. Since an album has multiple tracks, we may want to enumerate through it; however, it is also very similar to our Single class. In our design, we may then inherit from Single, but also choose to mix in Enumerable, a mixin that defines mechanisms for enumerating an object.

2137 questions
1319
votes
18 answers

What is a mixin and why is it useful?

In Programming Python, Mark Lutz mentions the term mixin. I am from a C/C++/C# background and I have not heard the term before. What is a mixin? Reading between the lines of this example (which I have linked to because it is quite long), I am…
TarkaDaal
  • 18,798
  • 7
  • 34
  • 51
270
votes
6 answers

Sass - Converting Hex to RGBa for background opacity

I have the following Sass mixin, which is a half complete modification of an RGBa example: @mixin background-opacity($color, $opacity: .3) { background: rgb(200, 54, 54); /* The Fallback */ background: rgba(200, 54, 54, $opacity); } I have…
Rick Donohoe
  • 7,081
  • 6
  • 26
  • 38
171
votes
2 answers

Mixins vs. Traits

What is the difference between Mixins and Traits? According to Wikipedia, Ruby Modules are sort of like traits. How so?
KaptajnKold
  • 10,638
  • 10
  • 41
  • 56
136
votes
7 answers

ruby inheritance vs mixins

In Ruby, since you can include multiple mixins but only extend one class, it seems like mixins would be preferred over inheritance. My question: if you're writing code which must be extended/included to be useful, why would you ever make it a class?…
Brad Cupit
  • 6,530
  • 8
  • 55
  • 60
134
votes
10 answers

Mixin vs inheritance

What is the difference between a mixin and inheritance?
Johnd
  • 6,441
  • 9
  • 29
  • 22
117
votes
2 answers

Using mixins vs components for code reuse in Facebook React

I'm beginning to use Facebook React in a Backbone project and so far it's going really well. However, I noticed some duplication creeping into my React code. For example, I have several form-like widgets with states like INITIAL, SENDING and SENT.…
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
115
votes
3 answers

Syntax for if/else condition in SCSS mixin

Hi I'm trying to learn SASS/SCSS and am trying to refactor my own mixin for clearfix what I'd like is for the mixin to be based on whether I pass the mixin a width. thoughts so far (pseudo code only as I will be including other mixins) @mixin…
clairesuzy
  • 27,296
  • 8
  • 54
  • 52
108
votes
4 answers

Inheriting class methods from modules / mixins in Ruby

It is known that in Ruby, class methods get inherited: class P def self.mm; puts 'abc' end end class Q < P; end Q.mm # works However, it comes as a surprise to me that it does not work with mixins: module M def self.mm; puts 'mixin'…
Boris Stitnicky
  • 12,444
  • 5
  • 57
  • 74
98
votes
7 answers

When to use mixins and when to use interfaces in Dart?

I'm very familiar with the concepts of interfaces and abstract classes, but not super familiar with the concepts of mixins. Right now, in Dart, every class A defines an implicit interface, which can be implemented by another class B by using the…
nbro
  • 15,395
  • 32
  • 113
  • 196
98
votes
7 answers

What are Mixins (as a concept)

I'm trying to get my head around the Mixin concept but I can't seem to understand what it is. The way I see it is that it's a way to expand the capabilities of a class by using inheritance. I've read that people refer to them as "abstract…
matanc1
  • 6,525
  • 6
  • 37
  • 57
97
votes
7 answers

Abstract classes vs. interfaces vs. mixins

Could someone please explain to me the differences between abstract classes, interfaces, and mixins? I've used each before in my code but I don't know the technical differences.
Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
81
votes
2 answers

Mixins vs composition in scala

In java world (more precisely if you have no multiple inheritance/mixins) the rule of thumb is quite simple: "Favor object composition over class inheritance". I'd like to know if/how it is changed if you also consider mixins, especially in…
Sandor Murakozi
  • 4,372
  • 25
  • 27
73
votes
11 answers

Is it possible to implement mixins in C#?

I've heard that it's possible with extension methods, but I can't quite figure it out myself. I'd like to see a specific example if possible. Thanks!
Stewart Johnson
  • 14,281
  • 7
  • 61
  • 70
72
votes
4 answers

Are Mixin class __init__ functions not automatically called?

I'd like to use a Mixin to always add some init functionality to my child classes which each inherit from different API base classes. Specifically, I'd like to make multiple different child classes that inherit from one of these different…
B Robster
  • 40,605
  • 21
  • 89
  • 122
67
votes
2 answers

Mixing multiple traits in Scala

Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and Types. Suppose I have the traits Student, Worker, Underpaid, and Young. How could I declare a class (not instance), CollegeStudent, with all these traits? Note: I am…
Daniel Ribeiro
  • 3,110
  • 3
  • 23
  • 49
1
2 3
99 100