title: Elixir macros and bind_quoted
body: <p>I have a macro that defines a module like so.</p>
  
  <pre><code>defmodule Bar do
    def bar do
      IO.puts "I am #{inspect __MODULE__}"
    end
  end
  
  defmodule MacroFun do
  
    defmacro define_module(name) do
      quote do
        defmodule unquote(name) do
          import Bar
          def foo do
            bar
            IO.puts "I am #{inspect __MODULE__}"
          end
        end
      end
    end
  
  end
  
  defmodule Runner do
    require MacroFun
  
    def run do
      MacroFun.define_module Foo
      Foo.foo
    end
  
  end
  
  Runner.run
  </code></pre>
  
  <p>The output of running this is:</p>
  
  <pre><code>I am Bar
  I am Runner.Foo
  </code></pre>
  
  <p>Which makes sense; <code>MacroFun.define_module</code> was called in <code>Runner.run</code> so the module was defined and thus nested under the <code>Runner</code> module.</p>
  
  <p>But now if I change <code>MacroFun.define_module</code> to use the <code>:bind_quoted</code> option:</p>
  
  <pre><code>  defmacro define_module(name) do
      quote bind_quoted: [name: name] do
        defmodule name do
          import Bar
          def foo do
            bar
            IO.puts "I am #{inspect __MODULE__}"
          end
        end
      end
    end
  </code></pre>
  
  <p>The output now becomes:</p>
  
  <pre><code>I am Bar
  I am Foo
  </code></pre>
  
  <p>Why??</p>