Arc Forumnew | comments | leaders | submitlogin
7 points by sacado 6101 days ago | link | parent

Macros have to be defined before they are used in a function, because they are expanded during the definition of the function.

Example :

  (def hello () (blankpage (pr "Hello world")))
  => def is a macro, evaluated as (set hello (fn () (blankpage (pr "Hello world"))))
Once the function is "compiled", no macro expansion will ever happen. So, when you call 'hello, it's looking for a function named 'blankpage and cannot find it.

  (mac blankpage body `(tag html (tag body ,@body)))
  => new macro saved

  (def hello () (blankpage (pr "Hello world")))
  => def and blankpage are macros, expansion performed, evaluated as 
  => (set hello (fn () (tag html (tag body (pr "Hello world")))))
  => tag is a macro, so another expansion has to be performed, etc.
Here, all macros are evaluated, only function calls are left in the compiled form. Thus, if you redefine 'blankpage or any other macro, don't forget to redefine all associated functions too.

But it does not apply to functions. They can be defined in any order.



2 points by croach 6101 days ago | link

That's quite interesting. After reading through your explanation it makes perfect sense why I am running into this problem. Thanks for the answer and for such a fast reply to my query.

-----