On Fri, Jan 22, 2016 at 3:43 PM,  <[email protected]> wrote:
> Hi,
>
> I have this quite weird situation:
>
>
> module Mod
>
> export @generate_macro_f, @generate_function_g, h
>
> macro generate_f()
>     return esc(quote
>         macro f(name)
>             return quote
>                 print($name)
>             end
>         end
>     end)
> end
>
> macro generate_g()
>     return esc(quote
>         function g()
>             @f("hello")
>             print(" world\n")
>         end
>     end)
> end
>
> function h()
>     @generate_f()
>     @generate_g()
> end
>
> end
>
> using Mod
>
> h()
> g()
>
> I want the function g to be generated using only one call to a function,
> here h (or possible a macro...), outside the module. Such a code would
> return an error saying that @f is not defined, and I don't understand why
> since I escaped the quote block of generate_f. This example is of course
> oversimplified but @f needs to be called inside g.

It's a little bit unclear what you actually want to do but a few
clarification below.

1. Macro expansion happens right after parsing. therefore, @generate_g
is expanded (and failed) right after h() is defined and before the
module definition finishes.
2. Unless you call `eval` with the correct module in `h()`, you won't
be defining any toplevel values in the module that calls `h()`. In
another word, the function you defined is a local variable and you are
not defining it in module you want to call it. Also, I don't think
macro definition is allowed in local scope and you should get an error
about that when you call `h()`

>
> Any thought?
>
> Many thanks,

Reply via email to