That's one of the really fiddly parts of macro hygiene -- the two uses of esc have a different meaning and apply to different contexts. In the first, it allows the function definition to escape into the surrounding environment. In the second, it changes the lookup scope of the global variable from the callee's module to the callers module or function.
Is the 0.4 tag already applied to the PR that changes this esc stuff to tagged symbols? On Tuesday, August 12, 2014, Stefan Karpinski <[email protected]> wrote: > I think you probably should escape both instances of $OP not just the > first one. > > > On Tue, Aug 12, 2014 at 1:55 PM, Philippe Maincon < > [email protected] > <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote: > >> Thank you again, Jacob! >> >> I pondered that under the shower, and it did the trick. For record, if >> somebody wants to generate a function from a macro in a function, here is a >> little example. Case closed. >> >> Philippe >> >> module moo >> >> importall Base # need to import Base.cos, Base.sin to add methods >> to it >> >> export Typ # export all that is to be public >> >> type Typ # public, because exported >> >> x >> >> end >> >> cos(a::Typ) = cos(a.x) # add method to base function - this does NOT >> require any export out of this module or import by the user >> >> macro makefoo(OP) >> >> return quote >> >> $(esc(OP))(a::Typ)= $OP(a.x) # add method to base function. Note >> the $(esc(OP)) to prevent macro hygiene from changing the name of generated >> function >> >> end >> >> end >> >> println(macroexpand(:(@makefoo(sin)))) >> >> @makefoo(sin) >> >> end >> >> >> importall moo >> >> println(methods(cos)) >> >> println(methods(sin)) >> >> >> >
