[Update: absz comments that this is already fixed in Anarki.] Suppose I want to do something like (mac addn (n) (fn (x) (+ x n)))
now, this is a silly example, because I could just say (def addn (n) (fn (x) (+ x n)))
or, if I really wanted to make addn a macro, (mac addn (n) `(fn (x) (+ x ,n)))
but for the sake of argument let's say that I have some time consuming process that generates a function, and I want to do that at macro expansion time. (mac addn (n) (really-hard-working-optimizing-compiler n))
OK, so back to my first example, arc> (mac addn (n) (fn (x) (+ x n)))
#3(tagged mac #<procedure>)
arc> (addn 4)
Error: "Bad object in expression #<procedure>"
arc2 doesn't like macros expanding to function values. Macros can expand into expressions like (fn ...) that generate function values, but you can't have a macro expand into an actual function value.Ah, but what if we had a function that will create a global variable for us? (def global (value)
(let var (uniq)
((scheme xdef) var value)
var))
(If you're using Anarki, replace "scheme" with "$"; with plain arc2, export xdef from Scheme using xdef inside of ac.scm). arc> (global 123)
gs1443
arc> gs1443
123
Now we can say (mac addn (n)
(global (fn (x) (+ x n))))
and it works! arc> (addn 4)
#<procedure>
arc> ((addn 4) 5)
9
|