3 ms·
I was ready to be excited from the title, but was utterly disappointed :( IMO these aren’t macros in the Lisp-sense of the word (or Rust, or even C); yeah they
by goranmoomin 3y ago
I was ready to be excited from the title, but was utterly disappointed :(
IMO these aren’t macros in the Lisp-sense of the word (or Rust, or even C); yeah they run code at compile time, but that’s where the common ends.
Macros should be able to apply syntactic transformation on the code. Lisp is famous for allowing that by representing code as lists. Rust has a compiler-level API to give tokens and run arbitrary code, then spit new tokens out. C macros operate on the tokens level, so with enough magic you can transform code to the shape you want.
This… isn’t any of that.
A pretty good example (and something I’m still sad that it didn’t take off) of macros in JS is Sweet.js[0]. Babel macros[1] are a bit higher level, where macros require the input to already be a valid AST, but that’s also something I’d call macros.
This… I’d say it’s more of a compile time code execution feature, not a macro feature.
[0]: https://www.sweetjs.org/ https://www.sweetjs.org/
[1]: https://babeljs.io/blog/2017/09/11/zero-config-with-babel-macros https://babeljs.io/blog/2017/09/11/zero-config-with-babel-ma...
- Jarred 3y agoAn earlier version of macros in Bun looked more like what you describe. We nerfed it because the API was too complicated. But we probably will revisit in the future. https://gist.github.com/Jarred-Sumner/454da846d614f7bb4bcceb6ddb0e8515 https://gist.github.com/Jarred-Sumner/454da846d614f7bb4bcceb...
- namuol 3y agoCame here to suggest renaming this feature for the reasons described here. Ideas are cheap, and naming is hard, but I like Zig’s “comptime” term, which is closer to this than “macro”. “comptime” distinguishes it from C preprocessor stuff, but also describes what it actually does more clearly.
- mhio 3y agoI've been using ts-morph for this recently, the accompanying AST viewer makes getting started easy. https://github.com/dsherret/ts-morph https://github.com/dsherret/ts-morph https://ts-ast-viewer.com https://ts-ast-viewer.com
- love2read 3y agoOut of curiosity, what have you made?
- mhio 3y agoIt was mostly utilitarian, modifying the outputs of some code generation tools where maintaining a fork was going to be difficult. The most recent was munging some file upload details where the OpenAPI definition is incomplete (or only recently defined). Also adding some aspect oriented changes across the code (audit/log/debug).