I have started to use Metaprogramming now. While my code is running, I noticed that there are some things that I do not understand. My understanding is that at the simplest level macros return an expression which is then evaluated. So, I tried to define a variable b and set its value to one using a macro:
macro test(x)
quote
b=1
end
end
When I then run in REPL:
b
Then I get an error message:
ERROR: UndefVarError: b not defined
The macro seems to return 1.
On the other hand if I type,
eval(Meta.parse(“b=1”))
Then b is defined in the REPL.
I would have expected the macro and the eval call to have the same effect. Apparently, there is something I am not understanding here…
Thanks!
4 posts - 3 participants