Re:Sweet.js – Hygienic Macros for JavaScript
by FluffyKitty on Wednesday July 17, 2019 @06:01PM
(#20464494)
I actually work on a JS project that needed to heavily rely on macros to reduce build file size (the project is from before ES6 and rollup.js). I ended up going to MetaScript [1] as all of its syntax was based on comments. So all the other tooling and code highlighting worked without issue.
Re:Sweet.js – Hygienic Macros for JavaScript
by btown on Wednesday July 17, 2019 @06:45PM
(#20464797)
Typescript doesn't officially support macros yet, but if you want to live dangerously (which you would if you were choosing to use Sweet.js!) you can check out https://github.com/cevek/ttypescript#transformers :)
Re:Sweet.js – Hygienic Macros for JavaScript
by ar_lan on Wednesday July 17, 2019 @05:41PM
(#20464352)
On the README:
> Currently, Sweet should be considered experimental and under heavy development (re-development more like). As such, the API will be undergoing a bit of churn until probably the end of the year. So, probably best not to try Sweet in production systems just yet. If you're interested in helping out though we'd love to have you!
Project last updated 2 years ago. I think this is dead.
Re:Sweet.js – Hygienic Macros for JavaScript
by slaymaker1907 on Wednesday July 17, 2019 @06:29PM
(#20464687)
Racket likely has the most sophisticated (mostly) hygienic macro system out there. In particular, syntax parameters are a really neat feature which lets you implement things normally requiring breaking hygiene in a hygienic way. For example, implementing early return using return as a keyword can be done using a syntax parameter which allows for nesting, renaming the “return” if you want, and composing it with other macros which utilize their own “return” keyword.
Re:Sweet.js – Hygienic Macros for JavaScript
by Quekid5 on Wednesday July 17, 2019 @07:06PM
(#20464943)
Racket truly has some great stuff wrt. macros -- I'm not sure if it was invented as a research vehicle for macros specifically[0], but they've even gone as far as implementing fairly advanced type systems as macros[1].
Besides the one I've linked, they have lots of interesting papers around the macro system: the handling of 'stages', the modularity, etc. Basically just google "Racket [something]" and go from there :)
[0] Probably more just a general 'extensible languages' type thing?
Re:Sweet.js – Hygienic Macros for JavaScript
by quelltext on Wednesday July 17, 2019 @09:43PM
(#20465917)
I only glanced over the paper but its novelty seems to lie in the turnstile language itself, i.e. the design of a metalanguage to define EDSLs that have a typesystem.
Yes, it is built using macros and I assume cleverly uses whatever macro system features Racket provides so there is possibly something novel there as well.
However, the fact that it can be done or has been done on its own is not quite so surprising to me. If you have the ability to inspect syntax you can obviously do something like that.
Re:Sweet.js – Hygienic Macros for JavaScript
by sawyerjhood on Wednesday July 17, 2019 @04:24PM
(#20463581)
It is a little bit of a shame that it doesn't seem to be under active development anymore. For personal projects, it would be fun to be able to write up little DSLs
Re:Sweet.js – Hygienic Macros for JavaScript
by hombre_fatal on Wednesday July 17, 2019 @04:59PM
(#20463952)
It seemed to undergo a major rewrite and then died.
I could also never really figure it out post-rewrite. Pre-rewrite, I had made some cool macros just out of intuition after reading some of its examples. Now, I can't get many of its own examples to compile in its live editor.
Too bad. It's about 1000x simpler than, say, implementing low-level Babel transformations.
Re:Sweet.js – Hygienic Macros for JavaScript
by yuchi on Wednesday July 17, 2019 @05:25PM
(#20464202)
I feel the pain. Things got a little bit easier with Babel Plugin Macros and Typescript (which forces you to build macros that adhere to a reasonable type system)
These "macros" take a different approach than Sweet.js: they're not syntax extensions, but rather code transformers that you import and use explicitly.
Re:Sweet.js – Hygienic Macros for JavaScript
by Lowkeyloki on Thursday July 18, 2019 @12:00AM
(#20466573)
After falling in love with Clojure and then Clojure being rejected by my company, I so wanted Sweet.js to become a thing. Macros allow for some incredible magic and amazing productivity, in my experience. Unfortunately, Sweet.js looks abandoned last I checked. And I'd be surprised if I could sell my company on it.
Re:Sweet.js – Hygienic Macros for JavaScript
by galaxyLogic on Wednesday July 17, 2019 @05:51PM
(#20464426)
I wonder, ES6 template strings allow any kind of string inside them which can be constructed based on existing values in the surrounding function. So I can give such a string as argument to a function which returns some non-string JavaScript value, perhaps a function.
Re:Sweet.js – Hygienic Macros for JavaScript
by Lerc on Wednesday July 17, 2019 @10:25PM
(#20466153)
Are there any active projects that do anything similar?
The two things I'd quite like to transform are
In a es2015 Class function if base identifier foo is a class field (in this or ancestor class) it becomes this.foo . Only for fields defined in the Class definition.
Re:Sweet.js – Hygienic Macros for JavaScript
by acjohnson55 on Wednesday July 17, 2019 @05:48PM
(#20464406)
Was a great project. I experimented with it about 5 years ago and really liked what I saw. But at the end of the day, it's not so fun having syntax that none of your tooling understands.
Re:Sweet.js – Hygienic Macros for JavaScript
by slaymaker1907 on Wednesday July 17, 2019 @06:35PM
(#20464723)
Hygienic macros are aware of what they are using. For example, if your macro uses leftpad, calling code does not need to import or be aware of leftpad. Also, consider the issues around writing a swap macro using eval, how do you introduce a temporary which is guaranteed to not conflict with the caller’s code? Hygiene solves this by saying the macro’s temp variable is not the same as the calling code’s temp variable.
2014: https://news.ycombinator.com/item?id=7978897
https://news.ycombinator.com/item?id=7219267
2012: https://news.ycombinator.com/item?id=4650929
https://news.ycombinator.com/item?id=4560691
// C program to illustrate a situation known as
// accidental capture of identifiers - an
// undesirable result caused by unhygienic macros
#define INCI(i) do { int x = 0; ++i; } while(0)
int main(void) { int x = 4, y = 8;
That can't be right.
The first case doesn't have any side effects. The second case does.
[1] https://github.com/dcodeIO/MetaScript
> Currently, Sweet should be considered experimental and under heavy development (re-development more like). As such, the API will be undergoing a bit of churn until probably the end of the year. So, probably best not to try Sweet in production systems just yet. If you're interested in helping out though we'd love to have you!
Project last updated 2 years ago. I think this is dead.
Besides the one I've linked, they have lots of interesting papers around the macro system: the handling of 'stages', the modularity, etc. Basically just google "Racket [something]" and go from there :)
[0] Probably more just a general 'extensible languages' type thing?
[1] http://www.ccs.neu.edu/home/stchang/pubs/ckg-popl2017.pdf
Yes, it is built using macros and I assume cleverly uses whatever macro system features Racket provides so there is possibly something novel there as well.
However, the fact that it can be done or has been done on its own is not quite so surprising to me. If you have the ability to inspect syntax you can obviously do something like that.
I could also never really figure it out post-rewrite. Pre-rewrite, I had made some cool macros just out of intuition after reading some of its examples. Now, I can't get many of its own examples to compile in its live editor.
Too bad. It's about 1000x simpler than, say, implementing low-level Babel transformations.
https://babeljs.io/blog/2017/09/11/zero-config-with-babel-ma...
https://github.com/kentcdodds/babel-plugin-macros
https://github.com/jgierer12/awesome-babel-macros
These "macros" take a different approach than Sweet.js: they're not syntax extensions, but rather code transformers that you import and use explicitly.
Would this qualify as a "hygienic macro"?
The two things I'd quite like to transform are
In a es2015 Class function if base identifier foo is a class field (in this or ancestor class) it becomes this.foo . Only for fields defined in the Class definition.
for (x of y) becomes for (let x of y)
I mean, I've seen languages like Rust using macros excessively, but why would you do so in JS?