简体   繁体   中英

How does the `quote` macro achieve interpolation?

How do the quote (declarative) and proc_quote (procedural) macros allow interpolation of local variables?

Since (declarative) macros are hygienic, I would expect that they wouldn't be able to access the environment they're called in.

Since (declarative) macros are hygienic, I would expect that they wouldn't be able to access the environment they're called in.

Why though?

vec![a, b, c]

works fine.

Macros being hygienic doesn't mean they can't access the environment they're called in (that would make them completely useless), it means they don't accidentally affect the environment they're called in, that is, the internal symbols of the macro will neither leak into nor conflict with environment symbols.

For instance using C macros, the names inside and outside of the macro live in the exact same namespace, so it's possible to have collisions and odd side-effects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM