minglecm / coffeescript-mixins

Package to allow mixins with CoffeeScript classes.
11 stars 2 forks source link

Call "super" in class hierarchy. #1

Open shaunc opened 9 years ago

shaunc commented 9 years ago

I understand that the default "super" from coffeescript access super in the Mixin's class heirarchy. However, it would be great if there were some way to use a mixin to access function overridden in the heirarchy it is mixed into. I have found the following workaround works if there is no other function in the Mixin heirarchy:

class Mixin
  foo: (bar)->
    @constructor.__super__.constructor.__super__.foo(bar)

(Going up 1 step gets you back to the same "foo" you started with.) This would suggest that you could provide some sugar for this... perhaps callable as (e.g.):

  foo: ()->
    @classSuper('foo', bar)
yangmillstheory commented 9 years ago

@shaunc I don't the mixin should know anything about the classes it's mixed into, like whether or not a superclass implementation exists (which means knowing where it is in the mixed-in hierarchy).

I think that the best way to accomplish what you're proposing is to let the mixing class call super (it's the right place, since it knows its own hierarchy) when overriding a mixin method, but to keep a reference to the mixin method.

In this way the mixin method wouldn't be invoked with super - which seems wrong, since mixins don't define an "is-a" relationship, but rather a "behaves-like-a" relationship). The mixin method could be invoked using a name-mangled reference, or automatically - the mixing class could inject custom behavior by using hooks provided by the mixin.