Skip to content
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

Interfaces / Traits / Multiple inheritance #636

Closed
cbart opened this issue Aug 20, 2010 · 1 comment
Closed

Interfaces / Traits / Multiple inheritance #636

cbart opened this issue Aug 20, 2010 · 1 comment

Comments

@cbart
Copy link

cbart commented Aug 20, 2010

As far as I can tell CoffeeScript supports only single inheritance (as for example Java). Multiple inheritance on the other hand can be painful, but it would be very nice if it supported traits (like in Scala - maybe not that powerfull, but kind of).
To be clear traits are like java interfaces, but unlike them - they provide implementation and attributes inside. However, like the Java interfaces, we cannot have actual instance of a trait. Also they are (in practice) quite like Python's mixins. For example:

class Vehicle
    constructor: (@power) ->
    getPower: ->
        @power

trait TurboBoost
    getPower: ->
        2 * super()

class Ferrari extends Vehicle with TurboBoost
    getPower: ->
        super() + 3

ferrari = new Ferrari 18
ferrari.getPower()

###
this would give: 2 * (3 + 18) = 42
###

There could be simple implementation of them as simple JS objects (like 'modules' in Prototype toolkit, which are mixed into classes at creation time).

@jashkenas
Copy link
Owner

This is a duplicate of the following tickets...

http://github.com/jashkenas/coffee-script/issues/issue/452
http://github.com/jashkenas/coffee-script/issues/issue/218

The reason why we don't have language support for mixins in CoffeeScript is because they're not supported by JavaScript -- you can't actually have more than one prototype chain on an object, meaning that the mixin's properties are simply copied over, which breaks both inheritance, and subsequent changes to the mixin. If you'd like to mix things in yourself, I recommend an extends function.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants