TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Mozilla Sweet.js: Extending JavaScript with Macros

21 pointsby EzGraphsover 12 years ago

4 comments

draegtunover 12 years ago
Perl6 also has a <i>macro</i> keyword for creating hygienic macros - <a href="http://en.wikipedia.org/wiki/Perl_6#Macros" rel="nofollow">http://en.wikipedia.org/wiki/Perl_6#Macros</a><p>For Perl5 you can use Devel::Declare (<a href="https://metacpan.org/module/Devel::Declare" rel="nofollow">https://metacpan.org/module/Devel::Declare</a>) or Devel::CallParser (<a href="https://metacpan.org/module/Devel::CallParser" rel="nofollow">https://metacpan.org/module/Devel::CallParser</a>) to achieve <i>macro</i> like effects.<p>For eg. Here's the Sweet.js <i>def add</i> macro using Devel::Declare...<p><pre><code> package MyDef; use strict; use warnings; use base 'Devel::Declare::MethodInstaller::Simple'; sub import { my $class = shift; my $caller = caller; my $arg = shift; $class-&#62;install_methodhandler( into =&#62; $caller, name =&#62; 'def', ); } sub parse_proto { my $ctx = shift; my ($proto) = @_; "my ($proto) = \@_;"; } 1; </code></pre> Then...<p><pre><code> use 5.016; use warnings; use MyDef; def add ($x, $y) { $x + $y } say add(1, 2); # =&#62; 3 </code></pre> So when the compiler sees the <i>def</i> keyword then MyDef takes over and converts the line into...<p><pre><code> sub add { my ($x, $y) = @_; $x + $y } </code></pre> ... then passes everything back to the perl parser to continue compiling the rest of the code.
davidjgraphover 12 years ago
Adds what to <a href="http://news.ycombinator.com/item?id=4650929" rel="nofollow">http://news.ycombinator.com/item?id=4650929</a>?
TazeTSchnitzelover 12 years ago
This seems to be blatantly plagiarised from somewhere, although I can't remember quite where.
CalvinCopyrightover 12 years ago
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...
评论 #4692565 未加载