hygiene

Can someone explain the concept of 'hygiene' to me (I'm a scheme programmer)?

若如初见. 提交于 2020-07-04 20:15:33
问题 So... I'm new to scheme r6rs, and am learning macros. Can somebody explain to me what is meant by 'hygiene'? Thanks in advance. 回答1: Hygiene is often used in the context of macros. A hygienic macro doesn't use variable names that can risk interfering with the code under expansion. Here is an example. Let's say we want to define the or special form with a macro. Intuitively, (or a b c ... d) would expand to something like (let ((tmp a)) (if tmp a (or b c ... d))) . (I am omitting the empty (or

How can I create hygienic identifiers in code generated by procedural macros?

谁都会走 提交于 2020-01-13 09:25:12
问题 When writing a declarative ( macro_rules! ) macro, we automatically get macro hygiene . In this example, I declare a variable named f in the macro and pass in an identifier f which becomes a local variable: macro_rules! decl_example { ($tname:ident, $mname:ident, ($($fstr:tt),*)) => { impl std::fmt::Display for $tname { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let Self { $mname } = self; write!(f, $($fstr),*) } } } } struct Foo { f: String, } decl_example!(Foo, f,

Collection of Great Applications and Programs using Macros

泪湿孤枕 提交于 2019-12-17 17:28:19
问题 I am very very interested in Macros and just beginning to understand its true power. Please help me collect some great usage of macro systems. So far I have these constructs: Pattern Matching: Andrew Wright and Bruce Duba. Pattern matching for Scheme, 1995 Relations in the spirit of Prolog: Dorai Sitaram. Programming in schelog. http://www.ccs.neu.edu/home/dorai/schelog/schelog.html Daniel P. Friedman, William E. Byrd, and Oleg Kiselyov. The Reasoned Schemer. The MIT Press, July 2005 Matthias

Why does macro hygiene not prevent collisions between multiple const definitions?

故事扮演 提交于 2019-12-04 04:46:15
问题 I thought "hygiene" would prevent collisions between X s defined within my macro m! but that turned out not to be the case. What am I misunderstanding? macro_rules! m { ($e:expr) => { const X: i32 = $e; }; } m!(0); m!(1); fn main() { m!(2); m!(3); } Playground Error message: error[E0428]: the name `X` is defined multiple times --> src/main.rs:3:9 | 3 | const X: i32 = $e; | ^^^^^^^^^^^^^^^^^^ | | | `X` redefined here | previous definition of the value `X` here ... 7 | m!(0); | ------ in this

Collection of Great Applications and Programs using Macros

我与影子孤独终老i 提交于 2019-11-28 02:49:07
I am very very interested in Macros and just beginning to understand its true power. Please help me collect some great usage of macro systems. So far I have these constructs: Pattern Matching: Andrew Wright and Bruce Duba. Pattern matching for Scheme, 1995 Relations in the spirit of Prolog: Dorai Sitaram. Programming in schelog. http://www.ccs.neu.edu/home/dorai/schelog/schelog.html Daniel P. Friedman, William E. Byrd, and Oleg Kiselyov. The Reasoned Schemer. The MIT Press, July 2005 Matthias Felleisen. Transliterating Prolog into Scheme. Technical Report 182, Indiana University, 1985.