On Sun, May 31, 2015 at 8:48 AM, Tom Lee <m...@tomlee.id.au> wrote: > No, in that example you create an anonymous function. I'm not certain on the > semantics, but "n" effectively points to this nameless function. Anonymous > functions are not as fast as "generic" ones (at least in 0.3, at least > sometimes) and you cannot add new methods to them for multiple dispatch (as > far as I know). > > 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. > > On Sunday, 31 May 2015 21:45:46 UTC+10, Kevin Squire wrote: >> >> Actually, it's name is "n": >> >> julia> function getfn() >> return function(); 1; end >> end >> getfn (generic function with 1 method) >> >> julia> const n = getfn() >> (anonymous function)
Well, it's anonymous as shown here. If you just want to give that function a name (i.e. make it a generic function). ```julia julia> function getfn() f() = 1 end getfn (generic function with 1 method) julia> getfn() f (generic function with 1 method) ``` This function is defined in the `getfn()` local scope and is not extending a global function with the same name nor is it bound to a global function for extending. Unless you want to define a closure, @eval is probably want you want. >> >> julia> n() >> 1 >> >> >> >> On Sat, May 30, 2015 at 5:09 PM, David P. Sanders <dpsa...@gmail.com> >> wrote: >>> >>> >>> >>> El domingo, 31 de mayo de 2015, 0:37:45 (UTC+2), Jameson escribió: >>>> >>>> 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() >>> >>> >>> This does not give quite the same answer, though, since the function does >>> not have a name. >>> Is there a way to specify the name of a generated function like this? >>> >>>> >>>> >>>> On Sat, May 30, 2015 at 2:30 PM David Gold <david....@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? >>>>>>> >> >