No hygiene

It's easy to write hygiene macros in Scala using reify and eval . But it is not always possible to use reify and eval .

So, if you do not use them, what are the rules to ensure the hygiene of the macro? And is there a way to check the macro to make sure that no bad hygiene has slipped through the cracks?

update In later milestones 2.10.0, Expr.eval was renamed to Expr.splice .

+6
source share
2 answers

Reify is hygienic as it stores characters along with identifiers and these trees.

If your macro extension result does not have characters attached to idents (for example, you only have Ident ("x") to point to something named x), then checking the macro extension types will bind x to what is in the area of ​​the call site (or if this area does not have x, you will get a compilation error).

In contrast, when your macro extension has characters for its identifiers, typechecker does not try to re-enable them and simply uses what it has. This means that when re-expressing an expression and using the result in a macro extension, it transfers its characters to the call site. Well, not all characters, for example. local variables or private / protected things cannot be referenced, but references to public declarations are retained.

The bottom line indicates that to check if your macro is hygienic, check to see if your identifiers have these characters either. You can achieve this by confirming or manually assigning symbols to your handmade trees.

+10
source

Since reify is a macro, I just look at its implementation to find out what it does.

+1
source

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


All Articles