Re: [julia-users] Re: "export" within a macro
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 < philippe.main...@gmail.com> 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)) > > >
[julia-users] Re: "export" within a macro
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))
Re: [julia-users] Re: "export" within a macro
You just need a little hygiene. Check out that section in the manual: http://docs.julialang.org/en/latest/manual/metaprogramming/#hygiene 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 - note position of export statement at top of module :) 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) OP = esc(OP) quote $OP(a::Typ) = $OP(a.x) # add method to base function - fails to export. Explicit export statement does not help end end @makefoo(sin) end importall moo On Tue, Aug 12, 2014 at 12:37 PM, Philippe Maincon < philippe.main...@gmail.com> wrote: > Thank you Jameson. > > Dear all > ...but I am still stuck. It's like that: the methods I generate by macro > in my module all overload Base functions (cos). If I write a new method > without using a macro, I don't need to export it explicitely (I imagine, > because I am not creating a new _function_). But if my method is macro > generate, it does not export: > > 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 - note position of > export statement at top of module :) > > 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 > > $OP(a::Typ)= $OP(a.x) # add method to base function - fails to > export. Explicit export statement does not help > >end > > end > > @makefoo(sin) > > end > > > importall moo > > println(methods(cos)) # 9 methods :) ... I know how to export functions > from a module > > println(methods(sin)) # 8 methods :( ... But not if I generated them by > macro > > Is that difference intentional? > > > Sorry Stephane, but the code of @deprecated is, after scrutiny, beyond me. > I'll have to take your word for now on the potential evils of exporting macro > generated function. But, then can you suggest a workaround? Can I export > the macro (I failed), and have the macro calls outside the module? I'd like > to keep the module - because it contains a bunch of private functions... > > > Philippe > >
[julia-users] Re: "export" within a macro
Thank you Jameson. Dear all ...but I am still stuck. It's like that: the methods I generate by macro in my module all overload Base functions (cos). If I write a new method without using a macro, I don't need to export it explicitely (I imagine, because I am not creating a new _function_). But if my method is macro generate, it does not export: 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 - note position of export statement at top of module :) 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 $OP(a::Typ)= $OP(a.x) # add method to base function - fails to export. Explicit export statement does not help end end @makefoo(sin) end importall moo println(methods(cos)) # 9 methods :) ... I know how to export functions from a module println(methods(sin)) # 8 methods :( ... But not if I generated them by macro Is that difference intentional? Sorry Stephane, but the code of @deprecated is, after scrutiny, beyond me. I'll have to take your word for now on the potential evils of exporting macro generated function. But, then can you suggest a workaround? Can I export the macro (I failed), and have the macro calls outside the module? I'd like to keep the module - because it contains a bunch of private functions... Philippe
[julia-users] Re: "export" within a macro
He he he! I missed the "return" statement in the doc (it's there, I cannot suggest any improvement, they are even titled "read the docs"), and from there I hacked my why to this ugly code. Well, macros are going to make sense now!!! I'll study the @deprecated code. Thank your both for your help! Philippe