-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Features: mixin
#81
Comments
I like the syntax you mentioned in the other thread: class Bar extends SomeBaseClass with Foo, Baz Why do we need a |
Indeed, I like the basic version only using the 'with' clause :)
Keep it concise, and I would extend this to objects as well as classes
perhaps?
…On Apr 21, 2017 6:15 PM, "Geoffrey Booth" ***@***.***> wrote:
I like the syntax you mentioned in the other thread:
class Bar extends SomeBaseClass with Foo, Baz
Why do we need a mixin keyword to declare a mixin? Couldn’t we declare it
with class like in the MDN example you link to?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#81 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABy6xzGH_Z8NxxRbsGFrBFLIqVoCiXjAks5ryVSggaJpZM4NE08I>
.
|
Yes.
Which you can already do in CoffeeScript. Why is The benefit of Of course, you could argue that You make a good point that |
Mixins would be a great addition IMO... and I really like the use of the word 'with'. Once upon a time, because of collision with the ECMAScript keyword
or
a new use of the word 'with' in CoffeeScript just seems natural to my eyes. I have to admit, I'd really, really like it. I could only assume that the mixin would be applied to the class prototype first, which would permit overrides, but it raises the question of what rules apply to overriding a mixin method?
Or would the rule be that mixin methods always override class methods? Or would static methods be ignored or added too? Or would another rule be that mixin methods are added in the order they are declared? That is, if two or more mixins had the same method, that each would override the others until the end of the mixin chain was reached (i.e. the last method implementation wins and is added to class prototype? What if instantiation was required by a mixin target and used its own variables?
Don't get me wrong, I think the path proposed before this comment is a good idea and I'd like to see it happen. I think the caveat would have to be that care would have to be taken by choice and use of mixin implementations. That is, its not really a problem of the CoffeeScript language, but a language feature that can be utilised by developers with a little forethought. |
Closing as there doesn’t appear to be much interest. Also, classes seem to be one of the most in-flux features in ECMAScript right now. I think we should avoid introducing new sugars on top of classes for the time being, because ECMA might introduce their own versions. Some future version of ECMAScript could very well introduce an equivalent of class mixins, and if we’ve already released our own sugar for it, and the two are incompatible? Then we’d have a repeat of the mess we went through with classes and CoffeeScript 2, when we had to rework CoffeeScript 1’s classes to be output using the |
Migrated to jashkenas/coffeescript#4974 |
The new ECMAScript classes lend themselves to a neat pattern for mixins. The idea is described pretty well here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes#Mix-ins.
A mixin is a function that takes a base class as a parameter, and returns a new class that extends it. Here is an example:
You can include
MyMixin
in your classes like so:This can be done in CoffeeScript too. What would be better, though, is if CoffeeScript supported it natively:
This could be extended to allow mixins to include each other:
would compile to
Unfortunately, this breaks backward compatibility by introducing two new keywords.
includes
could be replaced withwith
, which is already a reserved word, but that still leavesmixin
, which is probably a commonly used word. I'm not sure if there's any good alternatives, and I likemixin
because it's very clear about what it means.The text was updated successfully, but these errors were encountered: