(Sometimes the property that variable references copied from a macro call mean the same thing in the expansion is called "hygiene" and the property that variable references copied from a macro definition mean the same thing in the expansion is called "referential transparency." We include both properties in the term "hygiene.")
Specifically, a macro can bind temporary variables in its expansion without the risk of accidentally capturing references in the macro call to another binding with the same name. Furthermore, a macro can reference module bindings in its expansion without the risk of those references accidentally being captured by bindings of other variables with the same name that surround the macro call. A macro can reference module bindings in its expansion without worrying that the intended bindings might have different names in a module where the macro is called.
One way to implement this is for each template-element that is a name to be replaced in the macro expansion by a special token which plays the same grammatical role as a name but remembers three pieces of information:
In general one cannot know until all macros are expanded whether a name is a bound variable reference, a module binding reference, a variable that is being bound, or something that is not a binding name at all, such as a definition modifier or an intermediate word. Thus the information for each of those cases is retained in the special token. A named value reference and a binding connect if and only if the original names and the specific macro call occurrences are both the same. (In that case, the lexical contexts will also be the same, but this need not be checked.) A named value reference and a binding never connect if one originated in a template and the other originated in a macro call.
For purposes of hygiene, a pattern-keyword default is treated like part of a template, even though it is actually part of a pattern.
The mapping from getters to setters done by the
:= operator is hygienic. In all cases the setter name is looked up in the same lexical context and macro call occurrence as the getter name.
?= name in a template inserts into the expansion a reference to name, in the lexical context where the macro was called. It is as if name came from the macro call rather than from the template.
This allows exported macros to make use of private bindings without requiring these bindings to be exported for general use. The module that calls the macro does not need to import the private bindings used by the expansion.
Implementations must use some automatic mechanism for marking the bindings potentially referenced by macro expansions and must make such bindings available to any library where the macro is accessible. In general one cannot tell when a macro is defined what bindings are going to be referenced by macro expansions, because the right-hand sides of rewrite rules are not fully parsed until after a macro is called and expanded. However, an upper bound on this set of bindings can be computed.
Generated with Harlequin WebMaker