> If you have background in programming with Scala, could you please explain 
> how does a macro in Scala and Nim differ AND why aren't they more popular in 
> Scala?

I suspect it may be because the language is in a "Python 2 -> 3" bifurcation 
moment.

It has been a long time since I've personally programmed Scala, so doing a 
cursory glance (also take this with a grain of salt):

  * Scala's AST macros seem to be strong enough for [build-time binding 
construction](https://github.com/indoorvivants/sn-bindgen). This is common for 
languages that get to have a bit more of a robust macro system (even Rust, 
[with its weaker token tree model](https://github.com/rust-lang/rust-bindgen)) 
but one of the advantages of using, e.g. 
[Futhark](https://github.com/PMunch/futhark) or 
[Nimterop](https://github.com/nimterop/nimterop) is that, while these utilities 
shell-out to CLI tools much like those other utilities, they are more unique in 
that their Nim counterparts execute within Nim itself and not through a 
build-system because Nim allows for certain degrees of system interaction at 
compiletime - i.e. 
[`staticExec`](https://nim-lang.org/docs/system.html#staticExec,string,string,string)
 and the CTFE FFI.
  * Scala seems to have [a serialized AST/IR 
format](https://docs.scala-lang.org/scala3/guides/tasty-overview.html) and [a 
visualization tool for that format.](https://github.com/shardulc/tastyviz) I 
assume this serves a similar purpose as [MIR does in 
Rust.](https://blog.rust-lang.org/2016/04/19/MIR.html)
  * The API seems to be a little stricter up front in Scala's, e.g. Nim's macro 
API takes in `NimNode` frequently, but Scala's is more specific, 
`quotes.reflect.Term`, etc.
  * The thing that confuses me the most is the statement [that Scala's macros 
are expanded after all 
typechecking.](https://www.scala-lang.org/blog/2018/04/30/in-a-nutshell.html) 
One of Nim's key adds here is that it gives you the option for macro input in a 
lispier, DSL-focused way via `untyped`, or by `typed` if one prefers the input 
to semchecked prior to expansion for better errors. At one point it seems like 
[Scala 2 had something like 
this](https://www.scala-lang.org/api/2.13.0/scala-reflect/scala/reflect/macros/Typers.html)
 but it seems to have been eliminated as it exposed certain compiler internals. 
(Nim, by contrast, embraces this fact, although there is something of a break 
between `NimNode`, the macro AST, and the `PNode` API that exists internally to 
the compiler).
  * There are conflicting resources between Scala 2 and Scala 3 but the above 
suggests APIs like `sameType`, `typeof`, etc. are not available in Scala the 
same way they would be in Nim.
  * Nim intentionally bisects the macro API into templates and the more 
powerful (and dangerous?) AST macros. An argument can be made that the latter 
is essentially a cleaner quasiquote.


Reply via email to