Skip to content

Interfaces / Traits / Multiple inheritance #636

@cbart

Description

@cbart

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions