mixins

What are Mixins (as a concept)

一世执手 提交于 2019-12-17 03:45:35
问题 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 subclasses". Can anyone explain why? I'd appreciate if you'd explain your answer based on the following example (From one of my lecture slideshows): 回答1: Before going into what a mix-in is, it's useful to describe the problems it's trying to solve. Say you

Mixing in a module within Object causes all Objects to inherit that module's instance methods as singleton methods

百般思念 提交于 2019-12-14 03:17:57
问题 When attempting to add my own behavior to the Object class, I get undesired effects that don't occur when mixing the module into a user-defined class. module Entity def some_instance_method puts 'foo' end def self.secret_class_method puts 'secret' end module ClassMethods def some_class_method puts 'bar' end end def self.included( other_mod ) other_mod.extend( ClassMethods ) end end Now, I create class Bob such that it includes Entity . class Bob; include Entity; end; This yields the desired

Help Migrating mixins from Castle.DynamicProxy to DynamicProxy2

妖精的绣舞 提交于 2019-12-14 02:33:54
问题 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 : IHasShape, IHasColor { } Then assuming some obvious concrete implementations of IHasShape and IHasColor we would create a mixin like this: public IColoredShape

Controller @Mixin just works after recompile of running app

跟風遠走 提交于 2019-12-13 12:07:01
问题 Within my latest grails 2.3.0 project I'm using the @Mixin annotation to mixin a helper class to keep my controller more DRY. The mixin is just working if a made some changes within the controller to force a recompile of the controller. After the initial compile ( grails run-app ) the helper isn't mixed in - I get a MissingMethodException trying to access a method from the helper class. Here is my helper witin src/groovy : class ProjectHelper { def withProject(id, Closure c) { def project =

encapsulation for mixin's members in Scala

你说的曾经没有我的故事 提交于 2019-12-12 15:46:31
问题 Traits in Scala can be used as both mixins and interfaces. It leads to some inconsistence - if I want to close some method inside trait, I just can't do that: object Library { protected trait A { def a: Int = 5 } trait B extends A { private override def a: Int = super.a } //I want to close `a` memeber for all traits extending B; it's still possible to open it in some another trait `C extends A`, or even `Z extends B with C` } // Exiting paste mode, now interpreting. <console>:10: error:

Should a plugin adding new instance-methods monkey-patch or subclass/mixin and replace the parent?

元气小坏坏 提交于 2019-12-12 14:21:15
问题 As a simple example take a class Polynomial class Polynomial(object): def __init__(self, coefficients): self.coefficients = coefficients for polynomials of the form p(x) = a_0 + a_1*x + a_2*x^2 + ... + a_n*x^n where the list coefficients = (a_0, a_1, ..., a_n) stores those coefficients. One plugin-module horner could then provide a function horner.evaluate_polynomial(p, x) to evaluate a Polynomial instance p at value x , i.e. return the value of p(x) . But instead of calling the function that

Accessing a class's containing namespace from within a module

寵の児 提交于 2019-12-12 13:12:58
问题 I'm working on a module that, among other things, will add some generic 'finder' type functionality to the class you mix it into. The problem: for reasons of convenience and aesthetics, I want to include some functionality outside the class, in the same scope as the class itself. For example: class User include MyMagicMixin end # Should automagically enable: User.name('Bob') # Returns first user named Bob Users.name('Bob') # Returns ALL users named Bob User(5) # Returns the user with an ID of

Mixin's names parameterization with template argument

笑着哭i 提交于 2019-12-12 11:29:02
问题 Is it possible to generate a name for a function within a mixin template? Something like this: mixin template Generator(string name) { @property void mixin(name) pure nothrow // mixin(name) is not valid :( { //some cool stuff here } } 回答1: I'm hoping somebody can come up with something cleaner, but this should do what you want: mixin template Generator(string name) { mixin("alias " ~ name ~ " = _fun;"); @property void _fun pure nothrow { //some cool stuff here } } This unfortunately injects

SCSS @extend inside media query alternatives to avoid duplicate properties

时间秒杀一切 提交于 2019-12-12 05:26:57
问题 In SCSS , you can use @mixin and @extend to reuse code. For example: %even-row { background: pink; } @mixin even-rows-mixin($columns) { @for $i from 1 through $columns { &:nth-child(#{2 * $columns}n + #{$i + $columns}) { @extend %even-row; } } } li { @include even-rows-mixin(8); width: 12%; } Will generate: li:nth-child(16n + 9), li:nth-child(16n + 10), li:nth-child(16n + 11), li:nth-child(16n + 12), li:nth-child(16n + 13), li:nth-child(16n + 14), li:nth-child(16n + 15), li:nth-child(16n + 16

Namespaces and Mixins

↘锁芯ラ 提交于 2019-12-12 02:46:08
问题 I'm trying to clean up our namespaces. Basically our setup is somewhat like class myClass include myModule1 include myModule2 @important_var #critical instance variable Basically @important_var is a telnet handler that almost all methods need to get at. This works fine with the way it is setup right now. Unfortunately myModule1 & myModule2 are getting huge. So I keep running into namespace collisions for the methods. I'd love to access methods with a module wrapper eg: myClass_instance