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
59
votes
17 answers

Implement Mixin In Java?

Using Java 6, how can I implement a mixin? It is very easy and possible in Ruby. How can I get similar in Java?
Lennie
59
votes
4 answers

How to mixin and call link_to from controller in Rails?

This seems like a noob question, but the simple answer is eluding me. I need to call link_to in an ActionController method to spit out an HTML link. ActionView::Helpers::UrlHelper.link_to calls url_for, but this calls the AV module's version…
tribalvibes
  • 2,097
  • 3
  • 25
  • 30
58
votes
1 answer

Difference between @Delegate, @Mixin and Traits in Groovy?

Would someone explain when I would want to use Groovy Traits vs. Mixins (@Mixin) vs. Delegates (@Delegate)? Maybe some trade-offs and design concerns would help. They all seem to allow for reusing multiple "classes" of behavior. Thanks. :-) This SO…
Vahid Pazirandeh
  • 1,552
  • 3
  • 13
  • 29
57
votes
3 answers

Ruby 'module_function' vs. including module

In Ruby, I understand that module functions can be made available without mixing in the module by using module_function as shown here. I can see how this is useful so you can use the function without mixing in the module. module MyModule def…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
56
votes
4 answers

Ruby: module, require and include

I'm trying to use Ruby modules (mixins). I have test.rb: #!/usr/bin/env ruby require_relative 'lib/mymodule' class MyApp include MyModule self.hallo end and lib/mymodule.rb: module MyModule def hallo puts "hallo" end end Quite simple…
Dakkar
  • 5,682
  • 5
  • 22
  • 28
48
votes
3 answers

Dynamically mixin a base class to an instance in Python

Is it possible to add a base class to an object instance (not a class!) at runtime? Something along the lines of how Object#extend works in Ruby: class Gentleman(object): def introduce_self(self): return "Hello, my name is %s" %…
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
45
votes
1 answer

Is there a way to stub a method of an included module with Rspec?

I have a module that is included in another module, and they both implement the same method. I would like to stub the method of the included module, something like this: module M def foo :M end end module A class << self include M …
user3775153
  • 451
  • 1
  • 4
  • 5
44
votes
2 answers

What is the difference between a mixin and the decorator pattern?

The Decorator Pattern is dynamic extension-at-runtime of classes. It dynamically forms a is-a relationship. I started to wonder if I was over-complicating my API by using the Decorator Pattern after I got this answer about the difference between…
leeand00
  • 25,510
  • 39
  • 140
  • 297
43
votes
1 answer

Skipping an optional argument in Sass mixin

I have this mixin to handle a simple CSS3 linear gradient: @mixin linear-gradient($from, $to, $dir: bottom, $dir-webkit: top, $ie-filters: false) { background-color: $to; background-image: -webkit-linear-gradient($dir-webkit, $from, $to); …
Chris Pearce
  • 607
  • 1
  • 10
  • 15
40
votes
2 answers

Dynamic mixin in Scala - is it possible?

What I'd like to achieve is having a proper implementation for def dynamix[A, B](a: A): A with B I may know what B is, but don't know what A is (but if B has a self type then I could add some constraints on A). The scala compiler is happy with the…
Sandor Murakozi
  • 4,372
  • 25
  • 27
39
votes
6 answers

@import in @if statement in Sass

I want to load only the css needed for the login page for performance. On my other pages I want a grouped css file that will be cached on every page which contain all my css. I have the following…
woutvdd
  • 771
  • 1
  • 9
  • 20
38
votes
4 answers

Can a sass @mixin accept an undefined number of arguments?

I'm trying to create a sass mixin for transitions. This is what I have so far. @mixin transition($var) -webkit-transition: $var transition: $var I want to be able to pass it multiple arguments like this @include transition(color .5s linear,…
Ryan
  • 9,340
  • 5
  • 39
  • 42
38
votes
5 answers

What is C++ Mixin-Style?

I have just come across this keyword C++ Mixin-Style, do anyone know what this is? In this post, is has been answered as a design pattern. Is it the same design pattern as described in this document?
Bitmap
  • 12,402
  • 16
  • 64
  • 91
37
votes
5 answers

Mixing in a trait dynamically

Having a trait trait Persisted { def id: Long } how do I implement a method that accepts an instance of any case class and returns its copy with the trait mixed in? The signature of the method looks like: def toPersisted[T](instance: T, id:…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
36
votes
5 answers

How can I initialize a mixin's immutable data in Dart?

I am programming in Flutter using Dart 2.1.0, and come across this situation: mixin Salt { final int pinches; // Immutable, and I want to delay initialization. // Cannot declare constructors for mixin } class Meat with Salt { Meat(int…
Nick Lee
  • 5,639
  • 3
  • 27
  • 35