# Returning a module expression from a macro to be evaluated at a different module

**URL:** https://discourse.julialang.org/t/returning-a-module-expression-from-a-macro-to-be-evaluated-at-a-different-module/11738
**Category:** General Usage
**Created:** [June 17, 2018, 10:24pm UTC](https://discourse.julialang.org/t/returning-a-module-expression-from-a-macro-to-be-evaluated-at-a-different-module/11738 "2018-06-17T22:24:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [June 17, 2018, 10:24pm UTC](https://discourse.julialang.org/t/returning-a-module-expression-from-a-macro-to-be-evaluated-at-a-different-module/11738/1 "2018-06-17T22:24:21Z")

</div>

Hi Fellows,

I was trying to return a module expression to be evaluated at the top module of where i do the macrocall. Here is an example:

```nohighlight
module X
  export @cmodule
  is_module(a) = false
  is_module(e::Expr) = e.head == :module

  function make_top_level(e::Expr) 
      if e.head == :block && any( is_module.(e.args) )
        Expr(:toplevel, e.args...)
      elseif e.head == :block
        Expr(:block, make_top_level.(e.args)...)
      else
        e
      end
  end

  function create_module(name)
           quote
               module $name
               end
           end |> make_top_level
   end

   macro cmodule(name)
   		esc(create_module(name))
   end

   module Y
   		module Z
   			using X
   			#here if i do the following the new module will be X.Y.Z.MDD, but i want it to be X.MDD, How can i do that?
   			@cmodule MDD
   		end
   end
end

```

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [June 18, 2018, 7:27am UTC](https://discourse.julialang.org/t/returning-a-module-expression-from-a-macro-to-be-evaluated-at-a-different-module/11738/2 "2018-06-18T07:27:07Z")

</div>

You can’t do that. A macro cannot do more than you could by typing code. And you cannot define a module in `Y` and expect it to be in `X`. You could `eval` into any other module, as you found out in the [other thread](https://discourse.julialang.org/t/how-to-define-a-function-in-a-different-module-other-than-what-you-are-in-right-now/11736). But as stated there, that is bad style. I’m pretty sure that there is another solution to you problem though…
