Macro call vs macro definition environment in Julia

I am trying to understand from the documentation document Julia Metaprogramming in macrogigalia . The documentation states that

Julias Macro Extender solves these problems as follows. Firstly, the variables in the macro result are classified both local and global. A variable is considered local if it is assigned (and not globally declared), declared local, or used as the name of a function argument. Otherwise, it is considered global. Local variables are then renamed as unique (using the gensym () function, which generates new characters), and global variables are resolved in the macro definition environment. Therefore, both of the above problems are handled; macro locales will not conflict with any user variables, and time and println will refer to standard library definitions.

I wrote a small program to see if global variables were really allowed in the macro definition environment. I wrote the following:

f(x) = x + 100 macro g() # According to Julia docs, ... :(f(x) + 5) # ... f is global, x is local, right? end # if so, f should refer to the f above? (function main() local x = 3 f(x) = x - 100 # f in the call environment subtracts 100 println(@g()) # So why does this do -92? end)() 

If I understand Julia's docs correctly, part of the macroview should be to make sure that the functions called in the macro expression will not be captured by functions of the same name inside the caller’s environment. But this is exactly what happens here, the function f was the one that was defined locally.

I would have thought that I would have to use esc to use f in scope at the point of call. But this is not so, why?

And in addition, I noticed that the variable x inside the macro result is considered local, so for it it was necessary to create a new variable name gensymed, so as not to encounter x in the call macro. But this also did not happen!

How can I read the documentation to understand why esc should not be used here?

EDIT (CONFIRM)

  • When I state that f is global and x is local, according to the docs, I do this because I see that x used as an argument to a function. I understand that x not written or declared local, and it certainly looks global, but those documents that require function arguments should also be global!

  • I know the usual part of hygiene where gensymming locals ensures that variables with the same name in the context of macro-matching will not be inadvertently plastered. However, the documents claim that for functions, those that are visible in the context of macro definition are protected from the caller using their own. This is the part that makes no sense to me, because my experiment shows otherwise.

+5
source share
1 answer

This is mistake. This is an old Julia 0.5 problem and earlier, and it is fixed in Julia 0.6. See https://github.com/JuliaLang/julia/issues/4873 for more details.

+2
source

Source: https://habr.com/ru/post/1237071/


All Articles