You don't need to use a macro, a function can do this:

julia> function gf(n::Symbol = gensym()) 
       @eval function $(n)() 
       1
       end 
       end

I've also made the n argument optional, with gensym creating a unique name 
by default - the newly defined function is returned by gf, so you don't 
necessarily need to know its name. And of course if you give gf additional 
arguments you can programatically construct expressions based those and 
easily $ them into the @eval block. It's all very awesome.

But the point is a macro probably isn't appropriate for this type of thing. 
My understanding is that you should never use a macro if you can easily 
write an equivalent function.

Cheers,

Tom

On Thursday, 28 May 2015 23:26:39 UTC+10, Mauro wrote:
>
> Like this: 
>
> julia> macro gf(n) 
>        quote 
>        function $(esc(n))() 
>        1 
>        end 
>        end 
>        end 
>
> julia> @gf foo 
> foo (generic function with 1 method) 
>
> julia> foo() 
> 1 
>
> On Thu, 2015-05-28 at 12:06, Vasudha Khandelwal <[email protected] 
> <javascript:>> wrote: 
> > Can I use macros to generate functions with names passed as argument to 
> the 
> > macro? 
>
>

Reply via email to