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.

Stop Writing JavaScript Compilers, Make Macros Instead

242 pointsby jlongsterover 11 years ago

26 comments

syncover 11 years ago
I&#x27;d be afraid of inheriting any codebase that made extensive use of these JS macros.<p>You really have no idea what this is doing (the canonical example from sweetjs.org):<p><pre><code> class Person { constructor(name) { this.name = name; } say(msg) { console.log(this.name + &quot; says: &quot; + msg); } } </code></pre> It&#x27;s not javascript -- it&#x27;s your own made up &quot;language&quot; of macros.<p>I do believe with a steady hand this could lead to some great things, but the example on sweetjs.org seems rather heavy handed. Having different `class` macro definitions between codebases that use sweet.js will devolve into madness.
评论 #7026906 未加载
评论 #7025456 未加载
评论 #7026212 未加载
评论 #7025715 未加载
评论 #7026244 未加载
评论 #7027463 未加载
评论 #7025466 未加载
评论 #7025474 未加载
评论 #7026677 未加载
评论 #7030898 未加载
评论 #7028462 未加载
评论 #7025470 未加载
评论 #7028514 未加载
评论 #7028340 未加载
orthecreedenceover 11 years ago
Glad someone finally wrote a post about this. Coming from the lisp world, that&#x27;s the thing I constantly see missing from languages wrapping javascript. They generally change the syntax a bit, add a few features to minimize boilerplate in some areas, and call it a day. Why would I want to learn a new syntax to program in a language I already know well? Unless you give me the tools to write code that writes code, I&#x27;m not going to bother learning&#x2F;using a new javascript syntax.
评论 #7025533 未加载
sheetjsover 11 years ago
Since sweet.js code can&#x27;t be directly run in the browser, you need some &quot;compiling&quot; or &quot;interpreting&quot; step. It&#x27;s not obvious why this is any different from a compile-to-js language that had support for macros.<p>For example, someone wrote a macro system for coffeescript: <a href="https://github.com/mrluc/macros.coffee" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mrluc&#x2F;macros.coffee</a><p>Related discussions: <a href="https://github.com/jashkenas/coffee-script/pull/3171" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jashkenas&#x2F;coffee-script&#x2F;pull&#x2F;3171</a> <a href="https://github.com/gkz/LiveScript/issues/328" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;gkz&#x2F;LiveScript&#x2F;issues&#x2F;328</a>
评论 #7025495 未加载
评论 #7025945 未加载
评论 #7025507 未加载
cromwellianover 11 years ago
JavaScript compilers aren&#x27;t only for syntax sugar. Closure Compiler, Dart2JS, and GWT are <i>optimizing</i> compilers. Most large Google applications for example would be hideously large without an optimizing compiler.
评论 #7025883 未加载
chcover 11 years ago
This is basically just saying &quot;Choose Sweet.js in combination with some unknown array of macro libraries as your only language that compiles to JavaScript.&quot;<p>But because the article doesn&#x27;t really frame it this way, it doesn&#x27;t really explain to me why I would choose Sweet.js over ClojureScript or Scala.js or Black Coffee or whatever.
aryastarkover 11 years ago
I barely trust Guy Steele with macros. You know who I don&#x27;t trust with macros? Random JS coder dudes that haven&#x27;t learned what true hardship is, when debugging a macro that is 5 levels deep and subtly changes the language because, hey, I have a macro! And having a macro means using a macro.<p>Language design is hard. Scheme has gotten it wrong. Over. And over. And over. They didn&#x27;t get hygiene right for more than a decade. They <i>still</i> don&#x27;t have it right. But people think they can easily invent new syntax that somehow doesn&#x27;t do unexpected things. Your clever macro isn&#x27;t so clever when some poor son of a bitch has been beating his head on a bug all day long just to find out your macro-that-looks-like-a-function isn&#x27;t evaluating all its arguments because your macro-that-looks-like-a-function is really a function-that-is-a-macro. Don&#x27;t get me started on missing IF branches and undefined behavior...
kirtijthoratover 11 years ago
I would also recommend the WISP: A homoiconic JavaScript dialect with Clojure syntax, s-expressions and macros. More details at: <a href="https://github.com/Gozala/wisp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Gozala&#x2F;wisp</a>
skrebbelover 11 years ago
I strongly agree with everything the author writes, but I believe that sweet.js might not go far enough. For example, I am very fond of TypeScript. I&#x27;m also very fond of React&#x27;s JSX. Yet, I cannot mix the two, and neither JavaScript extension could be expressed as Sweet.js macros.
charlieflowersover 11 years ago
&gt;&gt;&gt;&gt; &quot;What if JavaScript actually adopted macros natively so that we never even had to run the build step?&quot;<p>Honestly, that&#x27;s pretty damn exciting. In my book, if JavaScript did that, it would very nearly complete a &quot;worst to almost-first&quot; transformation as a language. Starting with its early versions and all their ugly &quot;bad parts&quot;, and advancing all the way to one of the most exciting, expressive languages to work with.
Fishrock123over 11 years ago
I find the title kind of ironic, given that sweet.js is still a compiler. :P<p>However, it is true it would be better to have one standard-ish compiler that can just execute whichever macros you throw into it.<p>That being said, you still run into the issue that `sync` brought up.
rcarmoover 11 years ago
Best way I know of using macros in JS is <a href="https://github.com/Gozala/wisp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Gozala&#x2F;wisp</a>, which generates pretty readable code. If you&#x27;re going to pre-process, you might as well write in a nicer language...
ahunt09over 11 years ago
So, after puzzling around with the macro for swap a bit, it seems like macros are like functions, except without the scoping rules, which suggests to me that we now have two kinds of functions, to keep track of in the code, each of which look identical, but behave differently. swap(a,b) looks like a function, but can interact with all the variables in the same scope (or above) as when it&#x27;s called (is this the same for global functions?). Maybe I&#x27;m missing something big, but that seems dangerous when the macro is declared in another file, and I have to go figure out how it behaves.
评论 #7026364 未加载
chromanoidover 11 years ago
How do you deal with naming collisions of macros from different developers? Are you considering to introduce name spaces? To me macros as well as other surprise code modification stuff seem unfeasible in teams with more than a few developers. Don&#x27;t you think you risk maintainability with a huge macro apparatus?
评论 #7028389 未加载
lightbladeover 11 years ago
sweetjs was a good idea, but I&#x27;ve found it hard to use, or rather..it&#x27;s not the using part that&#x27;s hard, but writing your own macro.<p>sweetjs has this hygienic thing that renames identifiers. To prevent it from renaming, there are many hoops that you have to jump through to prevent it.<p>sweetjs introduces of a lot of new concepts. These concepts are not as easy to understand as the ones in C. The macros are certainly more powerful. It&#x27;s just that assuming it&#x27;s C-like macro will give you the wrong kind of expectation with sweetjs.
评论 #7025538 未加载
badman_tingover 11 years ago
I&#x27;m not super into the idea of code running on code because I likes it as simple as I can gets it. But, I know this notion is popular with lispers so I get it.
评论 #7025910 未加载
yuchiover 11 years ago
Did you had a look at [gorillascript](<a href="http://ckknight.github.io/gorillascript/" rel="nofollow">http:&#x2F;&#x2F;ckknight.github.io&#x2F;gorillascript&#x2F;</a>)? Its [prelude](<a href="https://github.com/ckknight/gorillascript/blob/master/src/jsprelude.gs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ckknight&#x2F;gorillascript&#x2F;blob&#x2F;master&#x2F;src&#x2F;js...</a>) is written in gorilla macros :)
nessus42over 11 years ago
I enjoyed the article. I just have a factual correction: Lisp had real macros as early as the 70s, not the 80s. (Maybe even earlier, for all I know.)
评论 #7026446 未加载
norswapover 11 years ago
If you need the same for Java: <a href="https://github.com/norswap/caxap" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;norswap&#x2F;caxap</a>
porkover 11 years ago
Very interesting! I&#x27;ve often thought that Javascript could actually be a pretty language with some syntactic sugar. It&#x27;d be even more interesting if you could run it client-side without preprocessing an sjs file into js first, sort of like how you can include LESS stylesheets and load a JS module to compile them in the browser.
voidrover 11 years ago
You won&#x27;t have good IDE support.<p>You won&#x27;t be able to directly debug your code.<p>Also it can be really fun to reason about nested macros.
vpjover 11 years ago
You can also use C macros with Javascript <a href="http://vpj.svbtle.com/coffeescript-and-macros-clean-and-fast" rel="nofollow">http:&#x2F;&#x2F;vpj.svbtle.com&#x2F;coffeescript-and-macros-clean-and-fast</a>
14113over 11 years ago
So wait, what does this have that&#x27;s better than C macros apart from hygiene? I&#x27;m afraid I really don&#x27;t follow.
smrtinsertover 11 years ago
There are many reasons why people are writing *-&gt;js compilers, lack of macros isn&#x27;t the least of the problems for js. Please keep the compilers coming, no productive coder really wants to write js.
评论 #7026580 未加载
Dewieover 11 years ago
I think things like syntactic sugar is perfectly fine, <i>as long as I can desugar it in a straightforward way</i>. I want to be able to programmatically desugar some piece of code, not have to Google it each time I am curious.<p>I think that if it is easy to investiage things like syntactic sugar, and not have it be buried in something like a compiler or lang spec, then DSL&#x2F;language implementers (and anyone else, if the language permits it) could get away with implementing things that objectively make the language more complex to deal with, because to decipher it is only a query away, anyway.
评论 #7027903 未加载
al2o3crover 11 years ago
Sup dawg, I heard you wanted to stop writing JS compilers and start using macros so here&#x27;s a JS compiler that implements macros.<p>&#x2F;head asplodes
johmacinduover 11 years ago
Don&#x27;t tell me what to do. If I want to write a JS compiler, I can write a JS compiler, and it&#x27;s nobody&#x27;s business to tell me not to.
评论 #7025403 未加载