mixin

What are some languages that currently support mixins?

Well obviously Ruby and Sass, but what are some other ones? ...

C++ dynamic type construction and detection

There was an interesting problem in C++, but it concerns more likely architecture. There are many (10, 20, 40, etc) classes that describe some characteristics (mix-in classes), for exmaple: struct Base { virtual ~Base() {} }; struct A : virtual public Base { int size; }; struct B : virtual public Base { float x, y; }; struct C : virt...

Help Migrating mixins from Castle.DynamicProxy to DynamicProxy2

I am trying to update some code from using DynamicProxy to DynamicProxy2. In particular we where using DynamicProxy to provide a mixin of two classes. The setup is something like this: public interface IHasShape { string Shape { get; } } public interface IHasColor { string Color { get; } } public interface IColoredShape : IHas...

Can you define <=> in Ruby and then have ==, >, <, >=, and <= defined automatically?

Here's part of my Note class: class Note attr_accessor :semitones, :letter, :accidental def initialize(semitones, letter, accidental = :n) @semitones, @letter, @accidental = semitones, letter, accidental end def <=>(other) @semitones <=> other.semitones end def ==(other) @semitones == other.semitones end ...

Ruby Mixins and Instance variables

Is there a best practice way to pass params to mixed-in methods? The class using the mixin could set up instance variables which mixed-in methods expect, or it could pass all necessary params as arguments to mixed-in methods. The background to this is that we have a Rails controller which does publishing of content - but other controll...

C++ mixins via templates: why doesn't this work?

I've got a interface that's implemented as an abstract base class with a number of pure virtual public methods. These pure virtual functions can be implemented using a template since the differences between subclasses aren't large - so my idea was to use multiple inheritance to mix-in the appropriately templated helper-class that provid...

Executing a mixin method at the end of a class definition

I have a Mix-in that reflects on the receiver class to generate some code. This means that I need to execute the class method at the end of the class definition, like in this trivially dumbed down example: module PrintMethods module ClassMethods def print_methods puts instance_methods end end def self.included(rece...

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 above signature, but I could not yet figure out how the implementation would look like -...

Using type parameters and mixins in Scala

EDIT 2: I managed to achieve the type safety I wanted in my exercise with RomanNumerals using a combination of mixins and type parameters with the code below. In essence what it does is after importing everything in RomanNumerals I am able to write L X V I, but not L L or X L X cause then I get a type mismatch compiler error (since tho...

strange inheritance in ruby mixins

Hi, I am wondering, why is an included module's methods mixed in to any subsequent class definitions (as if the class included it in itself)? module Foo def bar print "#{self}\n" end end class Bar end begin Bar.bar rescue NoMethodError puts "There is no Bar.bar\n" end include Foo bar Bar.bar Bar.new.bar prints: There...

Solving the mixin constructor problem in C++ using variadic templates

I've recently tackled the constructor problem, where various mixins classes that decorate each other (and a topmost host class) have different constructor signatures. To maintain a single constructor in the resulting decorated class, and without adding init functions, I've found the following solution. The only restriction it places on a...

Self type inheritance in scala

Say I have the following traits: trait A trait B { this: A => } trait C extends B // { this: A => } Compiler error: illegal inheritance; self-type C does not conform to B's selftype B with A As expected if I uncomment the self type annotation, the compiler is happy. I think it's quite obvious why C also needs this self type. What ...

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 scala? Are mixins considered a way of multiple inheritance, or more class composition? Is there...

What's the correct way to inject common properties into an activerecord class?

I planned on using this module (full exmpample here http://pastie.org/1098444) puts "Name_and_key was referenced." module Name_and_key def normalize(s) s.mb_chars.normalize(:kd).gsub(/[^\-x00-\x7F]/n, '').to_s end def name=(name) self[:name] = name self[:key] = normalize(name).downcase end def name self[:na...

What detectable differences are there between a class and its base-class?

Given the following template: template <typename T> class wrapper : public T {}; What visible differences in interface or behaviour are there between an object of type Foo and an object of type wrapper<Foo>? I'm already aware of one: wrapper<Foo> only has a nullary constructor, copy constructor and assignment operator (and it only ...

How to apply a "mixin" class to an old-style base class

I've written a mixin class that's designed to be layered on top of a new-style class, for example via class MixedClass(MixinClass, BaseClass): pass What's the smoothest way to apply this mixin to an old-style class? It is using a call to super in its __init__ method, so this will presumably (?) have to change, but otherwise I'd l...

Invoking interface extension methods from implementor is weird in C#

Invoking an extension method that works on a interface from an implementor seems to require the use of the this keyword. This seems odd. Does anyone know why? Is there an easier way to get shared implementation for an interface? This irks me as I'm suffering multiple inheritance/mixin withdrawl. Toy example: public interface ITest ...

Why does Ruby module inclusion exclude the module's singleton class?

When classes are inherited in Ruby the singleton classes are also inherited: class A def self.hello puts "hello" end end class B < A end B.hello #=> "hello" Yet with modules, this is not the case: module M def self.goodbye puts "goodbye" end end class A include M end A.goodbye #=> NameError To get around this ...

Getting a list of classes that include a module

I have a mixin for which I would like to get a list of all the classes that have included it. In the mixin module, I did the following: module MyModule def self.included(base) @classes ||= [] @classes << base.name end def self.classes @classes end end class MyClass include MyModule end This works pretty well: ...

Alternatives to abstract classes in Ruby?

I am new to Ruby. A simple example, what I need: class Animal abstract eat() class Cat < Animal eat(): implementation class Dog < Animal eat(): implementation In other words, the eat() method should be required for all the classes which extend Animal. In JAVA I would just use an abstract class, but after doing so...