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
35
votes
5 answers

Is it possible to use mixins in php

I came to know about mixins.So my doubt is, is it possible to use mixins in php?If yes then how?
NewUser
  • 12,713
  • 39
  • 142
  • 236
34
votes
2 answers

Abstract class + mixin + multiple inheritance in python

So, I think the code probably explains what I'm trying to do better than I can in words, so here goes: import abc class foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def bar(self): pass class…
mluebke
  • 8,588
  • 7
  • 35
  • 31
32
votes
2 answers

Less mixin with optional parameters

I have a Less mixin defined as: .fontStyle(@family, @size, @weight: normal, @style: normal, @color: #ffffff, @letter-spacing: normal) { font-family: @family; font-size: @size; color: @color; font-weight: @weight; font-style: @style; …
Justin
  • 42,716
  • 77
  • 201
  • 296
32
votes
2 answers

What is the difference between 'include' and 'prepend' in Ruby?

From the Module Module#append_features(mod) → mod => When this module is included in another, Ruby calls append_features in this module, passing it the receiving module in mod. Ruby’s default implementation is to add the constants, methods, and…
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
32
votes
1 answer

Breaking ruby module across several files

I have a ruby module that is supposed to wrap up quite a few classes module A class First #somemethods end class Second #somemethods end class Third #somemethods end end What i would like to do in rails is to break up…
Kapil
  • 319
  • 3
  • 5
31
votes
5 answers

Mixins with C# 4.0

I've seen various questions regarding if mixins can be created in C# and they are often directed to the re-mix project on codeplex. However, I don't know if I like the "complete interface" concept. Ideally, I would extend a class like so: …
ActionJackson
  • 373
  • 1
  • 4
  • 8
30
votes
4 answers

Using a mixin with a Django form class

I'm thinking about creating a mixin form class so that I can add a common set of fields to a variety of otherwise very different forms. Just using it as a base class won't work because I want to be able to use other forms as base classes like…
Jordan Reiter
  • 20,467
  • 11
  • 95
  • 161
30
votes
3 answers

Python Mixin - Unresolved Attribute Reference [PyCharm]

I am using a mixin to separate a range of functionality to a different class. This Mixin is only supposed to be mixable with the only child class: class Mixin: def complex_operation(self): return self.foo.capitalize() class A(Mixin): …
ikamen
  • 3,175
  • 1
  • 25
  • 47
30
votes
3 answers

Doing math on variable argument Sass mixins

I like to use rem units with pixel fallbacks for my CSS sizing and am trying to make mixins to help with that. For font-size, this is easy: @mixin font-size($size) { font-size: $size + px; font-size: ($size / 10) + rem; } But for padding,…
Doug Hamlin
  • 996
  • 1
  • 11
  • 17
29
votes
3 answers

How to mock Vue Mixins during unit testing using vue-test-utils and jest?

I have read the documentation for vue-test-utils and Jest, but I am still unsure about how to properly mock Vue mixins in a Vue component and test the component.
Madhu Sudhan Subedi
  • 469
  • 1
  • 6
  • 13
29
votes
3 answers

Nested mixins or functions in SASS

Some body know how can i use nested mixins or functions in SASS? I have something like this: @mixin A(){ do something.... } @mixin B($argu){ @include A(); }
iLevi
  • 936
  • 4
  • 10
  • 26
29
votes
2 answers

Using variables for CSS properties in Sass

I am writing a @mixin with some math in it that calculates the percentage width of an element, but since it is very useful I would like to use the same function for other properties too, like margins and paddings. Is there a way to pass the property…
Laura Silvani
  • 747
  • 3
  • 9
  • 18
28
votes
3 answers

How can I use mixins or modules in my controllers in Rails 3?

I have some behavior in my controller that I pulled out into a module in order to test better and re-use it in a few places. Two questions about this: Where is a good place to put my modules? They need to run in order to be available to the…
Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
28
votes
5 answers

Django: a class based view with mixins and dispatch method

Normally, I use a dispatch method of a class based view to set some initial variables or add some logic based on user's permissions. For example, from django.views.generic import FormView from braces.views import LoginRequiredMixin class…
cansadadeserfeliz
  • 3,033
  • 5
  • 34
  • 50
28
votes
5 answers

Can I simulate traits/mixins in Swift?

Does Swift have a way of mixing in traits, a la Scala? The section of the Swift book on using extensions to add protocols to existing classes comes tantalizingly close. However, since protocols can't contain an implementation, this can't be used to…
Bill
  • 44,502
  • 24
  • 122
  • 213