But "@eval" is still a macro, so it is even better to rewrite this without
that:
function getfn()
return function(); 1; end
end
const n = getfn()

On Sat, May 30, 2015 at 2:30 PM David Gold <david.gol...@gmail.com> wrote:

> Something to note about Tom's method is that the name function must be
> passed to gf as a symbol, unlike in the case of a macro. However, in most
> cases this slight difference probably will not warrant a macro.
>
>
> On Friday, May 29, 2015 at 8:58:56 PM UTC-4, Tom Lee wrote:
>>
>> 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 <vasudhakh...@gmail.com>
>>> wrote:
>>> > Can I use macros to generate functions with names passed as argument
>>> to the
>>> > macro?
>>>
>>>

Reply via email to