
Mozilla Sweet.js: Extending JavaScript with Macros - EzGraphs
http://www.infoq.com/news/2012/10/Mozilla-Sweetjs
======
draegtun
Perl6 also has a _macro_ keyword for creating hygienic macros -
<http://en.wikipedia.org/wiki/Perl_6#Macros>

For Perl5 you can use Devel::Declare
(<https://metacpan.org/module/Devel::Declare>) or Devel::CallParser
(<https://metacpan.org/module/Devel::CallParser>) to achieve _macro_ like
effects.

For eg. Here's the Sweet.js _def add_ macro using Devel::Declare...

    
    
      package MyDef;
      use strict;
      use warnings;
      use base 'Devel::Declare::MethodInstaller::Simple';
       
      sub import {
          my $class  = shift;
          my $caller = caller;
          my $arg    = shift;
       
          $class->install_methodhandler(
              into => $caller,
              name => 'def',
          );
      }
      
      sub parse_proto {
          my $ctx = shift;
          my ($proto) = @_;
          "my ($proto) = \@_;";
      }
      
      1;
    

Then...

    
    
      use 5.016;
      use warnings;
      use MyDef;
    
      def add ($x, $y) { $x + $y }
    
      say add(1, 2);   # => 3
    

So when the compiler sees the _def_ keyword then MyDef takes over and converts
the line into...

    
    
      sub add { my ($x, $y) = @_; $x + $y }
    

... then passes everything back to the perl parser to continue compiling the
rest of the code.

------
davidjgraph
Adds what to <http://news.ycombinator.com/item?id=4650929>?

------
TazeTSchnitzel
This seems to be blatantly plagiarised from somewhere, although I can't
remember quite where.

------
CalvinCopyright
Exactly why and how does this let me do stuff I couldn't do before with JS?
The example used here looks like something that could be done just as easily
with OOP at first glance...

~~~
draegtun
The examples show how new _syntax_ can be added to JS which is something you
won't be able to do in OOP.

For more examples of where macros can be useful see this previous HN post:
<http://news.ycombinator.com/item?id=3124920>

