I'll concede that if you know the function name at runtime, Mauro's 
solution may be a little cleaner, especially if it will be called a lot.

There are plenty of examples in Base of @eval being used to define 
functions, such as lines 11-18 
here: 
https://github.com/JuliaLang/julia/blob/861f02712eb4b41c08fed3f21c5a4206b8d669bc/base/int.jl
I've copied this pattern in my own code and found it extremely convenient. 
I can't think of any examples of macros in Base which define functions.

Cheers,

Tom


On Monday, 1 June 2015 02:35:28 UTC+10, Josh Langsfeld wrote:
>
> I don't think this is correct actually. With Julia's metaprogramming 
> abilities, all macros could be handled by runtime functions. 
>
> The rule I follow is that macros should be used when some processing needs 
> to be done at compile time / source-processing time. Usually, that's 
> exactly when you want to programmatically generate code for a function. In 
> particular, I thought it was widely regarded that calling on 'eval' was a 
> sign you should consider using macros for your problem if possible.
>
> So I think the first reply from Mauro is the most Julian solution. Unless 
> there's something really weird like the function name isn't known until 
> runtime, in which case no macro can work.
>
> On Sunday, May 31, 2015 at 5:48:41 AM UTC-7, Tom Lee wrote:
>>
>> ...
>>
>> Also, I should clarify that when I wrote "My understanding is that you 
>> should never use a macro if you can easily write an equivalent function" I 
>> meant you should not create a new macro to do something that can just as 
>> easily be done by a function - not that existing macros like @eval should 
>> be avoided.
>>
>

Reply via email to