We can check that all three macros work: julia> using. qualify the function name with the module name (-> in the example below).mark it with the global keyword (-> in the example below).There are (at least) two ways of doing this: When the macro expansion gets evaluated, Julia tries to interpret function T.f1 as a function name, which does not make sense => error julia> fooĮRROR: syntax: invalid function name "typeof(T.f1)()"Īll in all, if you surround your code in a quote.end block (which I would advise to do), you’ll have to make it so that Julia understands f1 as being a global variable. Let’s follow the same steps in the case of $(esc(f1)):į1 is evaluated in the context of the macro expansion, yielding the function T.f1 (not its name: the function itself)Įscaping the function doesn’t mean much nothing is done about it regarding hygiene ( EDIT: to be clear, and as noted by esc has no effect whatsoever here, and can be removed without changing anything) when the macro expansion gets evaluated (in the context of the macro call), :foo therefore refers to the function named foo in the current scope.this symbol :foo is escaped, meaning that it is marked to not be changed for hygiene purposes → it therefore reaches the macro expansion without modification.sym is evaluated in the context of the macro expansion, yielding the symbol :foo, because that’s what the macro argument was named.In the case of $(esc(sym)), which you got right: Let’s try and understand what happens with this whole $(esc(.)) syntax. What we see is that, instead of the name of f1, we got the function itself. (For comparison, the same output for which works) julia> foo))ġ: GlobalRef # (qualified) name Main.T.f1 Let’s inspect it further: julia> using MacroToolsġ: f1 (function of type typeof(Main.T.f1)) # The function itself not its name Rather, the presence of parentheses is a subtle indicator that the generated code is not really what you think it is. It is not really because of the presence of parentheses: as you can easily verify, parentheses surrounding the function name are allowed. The reason why macro m3 does not work is a bit tricky to explain. But I’m actually not sure whether this is intended or not. My guess is that this change in behavior comes from the implicit begin.end block generated by quote.end. But in macro m2, it is detected as being local and is renamed. In macro m1, the name f1 is considered to refer to a global variable. GDB can evaluate expressions containing macro invocations, show the result of macro expansion, and show a macros definition, including where it was defined. We see there that a symbol appearing in a macro is renamed ( gensymed, like var"#5#f1" in your example) if the corresponding variable is considered local. The relevant part of the documentation is the section about macro hygiene. I’m not sure I can explain everything, but let’s at least try.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |