-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple inheritance? Mixins? #452
Comments
Yes, mixins - I'm missing them too. |
So. JavaScript inheritance of properties through prototypes is only for single inheritance. You can have chains pointing back as long as you like, but there's only one prototype per-object. Any "mixin" functionality would be limited to what can be accomplished at runtime through helper functions, as mentioned above. It would be a blind copy of properties onto the object or prototype, not a true inclusion, where subsequent changes to the module would affect the mixed-in objects. For this reason, I don't think that it's in the domain of CoffeeScript. Is there a concrete proposal for what you'd like it to compile into? |
I would like
to copy the properties from the |
? |
Closing this one as a The idea with CoffeeScript classes is to expose the common pattern of use of JavaScript's prototype chains in a convenient way -- not to mimic classes from Ruby or another language. If you'd like to include methods from an external module into a class you have a number of options: you can delegate to the class, manually copy methods over, or use a helper function like Prototype's It's an orthogonal issue to classes ... you should equally well be able to "mixin" functions into a vanilla object, which is something that an |
Mootools solves this using an
This is very powerful and I use it quite a lot in my code. I personally think Coffee should have a second think about allowing this kind of construct.
From the docs: Extends The Class that this class will extend. i.e. proper inheritance Implements Implements is similar to Extends, except that it adopts properties from one or more other classes without inheritance. Useful when implementing a default set of properties in multiple Classes. |
We can reopen it if someone cooks up a patch they'd like to propose. |
If we had inline anonymous classes, it would be possible to implement mixins in user code while keeping them at the top of the class definition:
...although a class defined this way could not have a subclass containing |
+1 for some kind of mixin functionality. It seems awkward and un-coffee to create a class and then copy functions to the prototype after the definition. That functionality feels like it belongs in the class definition. |
+1 |
+1 |
+1 |
Sorry, folks, but |
@jashkenas Thanks you for you response and I'm sorry for my noisy comment... |
You can have a longer prototype chain, or am I doing something stupid?
|
@ricardobeat: Yes, an object can have a long prototype chain -- but each object in the chain will inherit from another object in the chain. If you instead have 2 existing objects, |
As a workaround, you can copy functions into a class on initialize with jQuery to get snapshot mixins using something like the following:
Not without limitation, but lets you "choose composition over inheritance". UPDATE: Read the mixin section on http://arcturo.github.com/library/coffeescript/03_classes.html, previous link was broken, thanks @countable :) |
hm, that link on Mixins in the last post is dead now, and this issue is closed but it comes up first in Google for "coffeescript mixins", so incase anyone else ends up here, I'll note the Spine framework has support which you can easily steal as an alternative to the @captainpete workaround.
and then to use it
|
|
I agree that one shouldn't mess with the inheritance chain, but I do think that coffee-script should provide the functionality that Spine does, namely including other classes. Let I hope you'll consider this. Thanks |
I totally agree with @jashkenas . Coffeescript should not, and not ever, mimic the (useful?) constructs of other languages. Some seem to confuse the intention of Coffescript with libraries such as Prototype, MooTools, jQuery, Spine et.al. Go write a library, or something. |
Also, this is 2 years old. |
Yes @Nami-Doc, but the issue still holds relevance, don't you think? I stumbled upon this thread today, investigating multiple inheritance in coffescript and javascript. It seems to me the only way to achieve multiple inheritance in Javascript is to be rather explicit ... |
Yes, that's why I linked coco's solution. |
ty @Nami-Doc . |
For me, it's not about other language constructs, but the Object Oriented paradigm, reusability and a way to code. Yes, we can live without it, but, something like Interfaces, that just have methods, or just signatures could be useful. Why not just put methods of "interface C" into a "class B" that extends "class A" and, if it isn't implemented, throws an exception?
Then, we will have
Or just make a
|
Hey folks, I got Mixins in CoffeeScript ;) |
Mixins still could be useful for specifying behaviours which do not require managing their own "state" — in the context of Backbone I can think of a ViewBehaviour which can provide some methods for View for turning I still do think that mixins should be implemented by mangling a prototype chain, so they easily can be stacked up into complex behaviours. I propose the following implementation as a draft:
So basically what I think could be useful as a language support —
|
I support closing this bug. Here's how I get mixins:
Yes, it makes the prototype chain longer and calling Foo methods from A is microscopically slower, but it has almost exactly the semantics I'd expect¹, and it's 6 simple lines. In fact, it's hard for me to imagine what I'd want any of the above-proposed syntactic sugar to compile into, if not this. ¹ I mean:
|
I found Ruby's acts_as pattern is really handy when doing Ruby on Rails work. I were long to bring it to JS. Inspired by your comments, I've put a npm module to provide a similar "composition over inheritance" way in coffee. And it can also check who-looks-like-who for duck typing |
I think you should post that on the mailing list/irc, that possibly can interest some people. (I'd like to reduce noise in old and closed issue, however :).) |
understand, thank you 😄 |
You can take a look at CoffeeClasskit. I made it with an eye on Ruby's modules system. It uses |
For some may be interesting this variant: https://gist.github.com/darrrk/75d6a6a4d431e7182888 |
I think as ES6 and proxies are coming, this should be reconsidered. |
Hi! Just wanted to bring awareness to a little module I made, which implements multiple inheritance: https://github.com/arximboldi/heterarchy The interesting thing about this implementation is that, unlike the mixin-based approaches discussed here so far, it supports cooperative multiple inheritance, in a way similar to how Python does it. It has some drawbacks, so I'm unsure it's appropriate for the core language, but it should help pythonistas to import their beloved patterns. Cheers! |
I'm loving the
extends
keyword, but in the course of developing a complex project, I really wish I could give a class functionality from multiple sources. Obviously I could do this by extending the prototype manually, but it feels like a hack to mix theextends
syntax with JavaScript-style prototype manipulation.For my purposes, it would be great to be able to say
but I realize that multiple inheritance is a huge headache, so Ruby-style mixins or Scala-style traits would probably be a better solution. The Scala syntax would be especially nice for CoffeeScript:
Obviously
with
is already a keyword, but its meaning in this syntax is unambiguous.Of course, this is something that a standard library could do, but it would be much clearer to have something like the above syntax rather than something like
What does everyone else think?
The text was updated successfully, but these errors were encountered: