title: What's wrong with this Julia swap! macro?
body: <p>I'm trying to write a simple <code>swap!</code> macro in Julia, to understand the macro system. Here's my code so far:</p>
  
  <pre><code>macro swap!(x, y)
      quote
          local tmp = $(esc(y))
          $x = $(esc(y))
          $y = tmp
      end
  end
  
  a = 1
  b = 2
  
  @swap!(a, b)
  
  # prints: a: 1, b: 2
  println("a: $a, b: $b")
  </code></pre>
  
  <p>This runs without error, but isn't actually changing the values. Julia doesn't seem to have a function that just expands macros without executing them (as far as I can see), so this is hard to debug.</p>
  
  <p>The equivalent quote in a REPL seems to work as expected:</p>
  
  <pre><code>julia&gt; a = 1
  1
  
  julia&gt; a_sym = :a
  :a
  
  julia&gt; a_sym
  :a
  
  julia&gt; b = 2
  2
  
  julia&gt; b_sym = :b
  :b
  
  julia&gt; eval(quote
         tmp = $a_sym
         $a_sym = $b
         $b_sym = tmp
         end)
  1
  
  julia&gt; a
  2
  
  julia&gt; b
  1
  </code></pre>
  
  <p>What am I doing wrong?</p>