let-go icon indicating copy to clipboard operation
let-go copied to clipboard

Top-level `do`-s don't support `defmacro` inside

Open nooga opened this issue 1 year ago • 0 comments

Consider:

(defmacro dude [] `(+ 1 2 3))
(dude) ;; => 6

However, wrapped in do:

(do
  (defmacro dude [] `(+ 1 2 3))
  (dude)) ;; => (+ 1 2 3)

This happens because it gets compiled into

  1. def dude fn
  2. set-macro! dude
  3. call dude

When executing this, we have already left the compiler so the fact that dude is a macro doesn't matter, it's just a plain function to the VM. The compiler didn't know (dude) is to be macro-expanded because set-macro! was only executed after compiler finished.

nooga avatar Dec 06 '22 20:12 nooga