Sunday, April 27, 2014

This is pretty sweet. http://sweetjs.org/

This is pretty sweet. http://sweetjs.org/
Mark Knichel, John Lenz Please bind me to a pole, so I don't go and abuse this :)

// Define the class macro here...
macro class {

  rule {
    $className {
        constructor $cparams $cbody
        $($mname $mparams $mbody) ...
    }
  } => {
    function $className $cparams $cbody
    $($className.prototype.$mname
      = function $mname $mparams $mbody; ) ...
  }
}

// An now classes are in JavaScript!
class Person {
  constructor(name) {
    this.name = name;
  }

  say(msg) {
    console.log(this.name + " says: " + msg);
  }
}
var bob = new Person("Bob");
bob.say("Macros are sweet!");

h/t Paolo Fragomeni 
http://sweetjs.org