Macro - with - how does it work?

I am trying to understand the Special Forms Macro’s, specificially the with macro. But all I see is:

 defmacro with(args), do: error!([args])

How does this work?

Kernel.SpecialForms are special. They are not expanded like regular macros, but instead understood by the compiler itself and transformed into themselfs.

If you consider macros beeing molecules, than specialforms are the atoms those molecules are made from.

3 Likes
1 Like

As stated above with is not a macro, it’s a special form.

Specifically macro’s are internally still just functions, and thus they have an arity. Special forms like with and for are multi-arity (which I really don’t like, but eh), so by their design you have to do things like:

with(
  x <- blah(),
  y <- blorp(),
  z <- bloop()
) do
  x + y + z
end

Notice how you have inconsistency with , on the trailing arguments and all. If this were a macro it would probably instead have been implemented as:

with do
  x <- blah()
  y <- blorp()
  z <- bloop()
  x + y + z
end

Or maybe something like:

with do
  x <- blah()
  y <- blorp()
  z <- bloop()
after
  x + y + z
end

Or so, both of which I find far more readable and less breakable than the version that uses ,'s everywhere.

And yes, someone can and has made such a macro. ^.^

1 Like