paiq / blackcoffee

CoffeeScript + hygienic macros
MIT License
105 stars 9 forks source link

Exposing utility and UTILITIES in nodes.coffee #6

Closed tgriesser closed 10 years ago

tgriesser commented 10 years ago

First, just wanted to say really great work on this fork.

I've recently found myself in need of the old extended functionality which was removed awhile back (ticket https://github.com/jashkenas/coffee-script/pull/1960), and I figured that this fork might be a good way to add it back in there. I found that exposing UTILITIES and utility in nodes.coffee helps in this case, allowing you to do something like:

macro ->

  # Correctly set up a prototype chain for inheritance, including a reference
  # to the superclass for `super()` calls, and copies of any static properties.
  # and adding back the "extended" functionality
  macro.UTILITIES.extends = -> """
    function(child, parent) {
      for (var key in parent) {
        if (#{macro.utility 'hasProp'}.call(parent, key)) child[key] = parent[key];
      }
      function ctor() { this.constructor = child; }
      ctor.prototype = parent.prototype;
      child.prototype = new ctor();
      if (typeof parent.extended === "function") parent.extended(child);
      child.__super__ = parent.prototype;
      return child;
    }
  """

Does this seem like a reasonable use case?

vanviegen commented 10 years ago

Interesting use case indeed! Even though I'd say it's a pretty ugly and unintuitive syntax, I do like the fact that the patch is trivial. :) I'm not really sure if we'll want to document this behavior, as programs would be very prone to break with minor CoffeeScript version updates, I'd imagine... More opinions on this are very welcome!