TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Mozilla Sweet.js: Extending JavaScript with Macros

21 点作者 EzGraphs超过 12 年前

4 条评论

draegtun超过 12 年前
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.
davidjgraph超过 12 年前
Adds what to <a href="http://news.ycombinator.com/item?id=4650929" rel="nofollow">http://news.ycombinator.com/item?id=4650929</a>?
TazeTSchnitzel超过 12 年前
This seems to be blatantly plagiarised from somewhere, although I can't remember quite where.
CalvinCopyright超过 12 年前
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 未加载