Comment

Comment by wizzwizz4

Oh, so it's like Rust's `macro_rules!` identifier rule?

Rust's `macro_rules!` macros operate on ASTs, so the `x` inside a macro definition is different to the `x` in the scope where the macro is used, even though the textual representation of the identifier the macro inserts into the place it's used might be identical. This means that:

  macro_rules! add_two {
      ($i: ident) => {
          let x = 2;
          $i += x;
      }
  }

  fn main() {
      let mut x = 5;
      add_two!(x);
      println!("{}", x);
  }
appears to become:

  fn main() {
      let mut x = 5;
      let x = 2;
      x += x;
      println!("{}", x);
  }
but it still prints 7, not 4, because they're two different `x`s. (Of course, Red has first-class metaprogramming, so it's a lot better.)
parent wizzwizz4 5 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton