On February 14, 2016 at 21:49:30, Julia Tylors 
([email protected](mailto:[email protected])) wrote:

> Hi fellows,  
>  
>  
> I was coding a macro, and I am having a prefix issue with functions.  
>  
> basically the problem is: Every single one of available functions (_ex_func) 
> which i want to be globally accessible are prefixed with the name of the 
> module(X) in which the macro f is defined  
>  
>  
> Here is the code example:  
>  
> julia> module X  
> export @f
> macro f(x)
> st = string("_",x)
> sy = symbol(st)
> esc(quote
> function ($sy)()
> println("st")
> end
>  
> function ($(symbol(string("_1",x))))()  
> ($sy)()
> println("sty")
> end
> export $sy
> export $(symbol(string("_1",x)))
> end
> )
> end
> @eval @f ex_func
> end
> X
>  
> julia> using X  
>  
> julia> _  
>  
> _1ex_func __precompile__ _ex_func  
> julia> _1ex_func.env.defs.func.code
> AST(:($(Expr(:lambda, Any[], Any[Any[],Any[],0,Any[]], :(begin # none, line 
> 12:
> (X._ex_func)() # none, line 13: # i want this to be not prefixed by X
> return (X.println)("sty")
> end)))))
>  
> julia> macroexpand(:(@f ex_func))  
> quote # none, line 7:
> function _ex_func() # none, line 8:
> println("st")
> end # none, line 11:
> function _1ex_func() # none, line 12:
> _ex_func() # none, line 13: # it seems OK, here!!!
> println("sty")
> end # none, line 15:
> export _ex_func # none, line 16:
> export _1ex_func
> end
>  
>  
>  
> as you may see , I may well define _ex_func in other modules and use it from 
> the function X._1ex_func().  
> But this prevents me doing it.
>  
> How can i solve this problem?  
>  
> Thanks

OH! I know this!

Actually, I do believe this is a bug in macro hygiene and have been meaning to 
file it. In the mean time, double-escaping should work for you:

    julia> module X
               macro p(y)
                 quote
                   println($y)
                 end
               end
               macro q(y)
                 quote
                   println(:($($y)))
                 end
               end
           end
    X
    
    julia> using X
    
    julia> test = "Hello, world"
    "Hello, world"
    
    julia> @X.p(test)
    ERROR: UndefVarError: test not defined
    
    julia> @X.q(test)
    Hello, world

Reply via email to