mixins

Difference between @Delegate and @Mixin AST transformations in Groovy

无人久伴 提交于 2019-12-03 10:39:58
What's the difference between @Delegate and @Mixin AST transformations in Groovy. Maybe my question has to do with OO and when apply different patterns, but I use both and I can achieve the same behavior. class Person { String name = "Clark" def walk() { "Walk" } } @Mixin(Person) class Superhero { def fly() { "Fly" } } def superman = new Superhero() assert superman.name == "Clark" assert superman.walk() == "Walk" assert superman.fly() == "Fly" class Person { String name = "Clark" def walk() { "Walk" } } class Superhero { @Delegate Person person def fly() { "Fly" } } def superman = new

EventEmitter and Subscriber ES6 Syntax with React Native

守給你的承諾、 提交于 2019-12-03 08:55:50
问题 I am trying to implement an EventEmitter/Subscriber relationship between two components in a react native class. I have seen referenced the following materials: React Native - Event Emitters by Colin Ramsay React Native - Call Function of child from NavigatorIOS These solutions are adequate for what I am trying to accomplish, however, they bother require the use of mixins: [Subscribable.Mixin] on the receiving component to work properly with Subscriber . Unfortunately, I am using ES6 and

In Python can one implement mixin behavior without using inheritance?

一世执手 提交于 2019-12-03 08:39:05
Is there a reasonable way in Python to implement mixin behavior similar to that found in Ruby -- that is, without using inheritance? class Mixin(object): def b(self): print "b()" def c(self): print "c()" class Foo(object): # Somehow mix in the behavior of the Mixin class, # so that all of the methods below will run and # the issubclass() test will be False. def a(self): print "a()" f = Foo() f.a() f.b() f.c() print issubclass(Foo, Mixin) I had a vague idea to do this with a class decorator, but my attempts led to confusion. Most of my searches on the topic have led in the direction of using

Understanding ruby metaprogramming using method_added to overwrite instance methods dynamically

一曲冷凌霜 提交于 2019-12-03 08:04:09
I have the following code from Programming Ruby 1.9 (slightly adapted) I just want to ensure my thought process is accurate module Trace def self.included(culprit) #Inject existing methods with tracing code: culprit.instance_methods(false).each do |func| inject(culprit, func) end #Override the singletons method_added to ensure all future methods are injected. def culprit.method_added(meth) unless @trace_calls_internal @trace_calls_internal = true Trace.inject(self, meth) #This will call method_added itself, the condition prevents infinite recursion. @trace_calls_internal = false end end end

How are Scala's traits not really traits?

一曲冷凌霜 提交于 2019-12-03 06:49:26
Someone recently told me that Scala's traits aren't "true" traits, and that they were really just mixins. Unfortunately, I didn't get the opportunity to ask him why. Does anyone have an idea what he meant? Edit: As a definition of "traits," I have been referring to Nathanael Schärli’s dissertation and concept paper introducing traits. One key feature that seems to be missing from most mixin and/or multiple inheritance implementations is the ability to rename methods when you import them to avoid collision/ambiguity. Can Scala do that? Steve McDowell One key different between mixins and traits

How to model mixins / multiple interfaces in Haskell?

断了今生、忘了曾经 提交于 2019-12-03 06:14:51
I came across this question on modeling inheritance in Haskell and it reminded me that I have a little more complicated version of the same problem. I'll adopt the example from there because it's easier than thinking up my own. Suppose your program contains a number of types: data Camera = Camera ... data Light = SpotLight ... | DirectionalLight ... data Object = Monster ... | Player ... | NPC ... Now you want to implement some basic physics, so you want them all to have a position and a velocity, say of some type Vec3 . One way to do this is to declare a Physical typeclass with pos and vel

Mixins, variadic templates, and CRTP in C++

我们两清 提交于 2019-12-03 06:00:28
Here's the scenario: I'd like to have a host class that can have a variable number of mixins (not too hard with variadic templates--see for example http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.144 ). However, I'd also like the mixins to be parameterized by the host class, so that they can refer to its public types (using the CRTP idiom). The problem arises when trying to mix the two--the correct syntax is unclear to me. For example, the following code fails to compile with g++ 4.4.1: template <template<class> class... Mixins> class Host : public Mixins<Host<Mixins>>... { public:

Can I simulate traits/mixins in Swift?

梦想与她 提交于 2019-12-03 05:39:40
问题 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 mix code into a class. Is there another way? 回答1: As of Swift 2.0, yes! Providing Default Implementations You can use protocol extensions to provide a default implementation to any method or property requirement of that protocol. If a conforming

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

那年仲夏 提交于 2019-12-03 05:38:15
问题 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 controllers, so I was thinking the config/initializers/ directory. That seems a little suspect to me though. lib/ ? How do I ensure the code gets run so the modules are available to include in my controllers? Thank you kindly sirs. 回答1: lib/ is an

LESS css set dynamic background image with mixin

試著忘記壹切 提交于 2019-12-03 05:29:29
I am using LESS CSS . I am currently using Mixins with variables. Something like this works okay : .border-radius (@radius) { border-radius: @radius; } #header { .border-radius(4px); } This is not : .bg-img(@img) { background-image:url(@img); } #logo { .bg-img("../images/logo.jpg"); } i have tried combinations of using ' & " in the background-image:url ('') & ("") but then it tries to get the image as images/@img instead of the image name. other wise it gives me an error of Cannot call method 'charAt' of undefined I think writing background-image:url() all the time is too tedious, is this