# Use constant variable from outer module in inner module

**URL:** <https://discourse.julialang.org/t/use-constant-variable-from-outer-module-in-inner-module/48032>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 9, 2020, 12:16pm UTC](https://discourse.julialang.org/t/use-constant-variable-from-outer-module-in-inner-module/48032 "2020-10-09T12:16:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [October 9, 2020, 12:16pm UTC](https://discourse.julialang.org/t/use-constant-variable-from-outer-module-in-inner-module/48032/1 "2020-10-09T12:16:29Z")

</div>

Hey community 🙂

I’ve got a question about modules. I want to use a field of an outer module in an inner module. How would I do that?

```julia
module A
const A_const = "stuff"
    module B
        do_stuff(A_const)
    end
end

```

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [October 9, 2020, 12:17pm UTC](https://discourse.julialang.org/t/use-constant-variable-from-outer-module-in-inner-module/48032/2 "2020-10-09T12:17:38Z")

</div>

Just figured it out 🙂  
Here is my solution.

```julia
module A
const A_const = "stuff"
    module B
        import ..A
        do_stuff(A.A_const)
    end
end

```

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [October 9, 2020, 7:03pm UTC](https://discourse.julialang.org/t/use-constant-variable-from-outer-module-in-inner-module/48032/3 "2020-10-09T19:03:10Z")

</div>

To be honest, this looks a bit fishy to me.

At some point you presumably want to have `using .B` in `A`. Then you get into circular references (`A` uses `B` and the other way around).

Usually one sees this the other way around, as in `A` uses `B`.

Of course I don’t know what you are trying to achieve, but perhaps a better alternative is

```julia
module A
  module C
    export x
    x = 1
  end
  using .C
  module B
    export y
    using ..C
    y = x + 1
  end
  using .B
  println(y)
end

```

Considering the bigger picture (again not knowing what your specific application looks like) I have found submodules to be more trouble than it’s worth for what I do. When modules get big enough to justify more structure, I prefer to make `B` into a separate package. But tastes vary.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 9, 2020, 10:54pm UTC](https://discourse.julialang.org/t/use-constant-variable-from-outer-module-in-inner-module/48032/4 "2020-10-09T22:54:59Z")

</div>

Just to give a different opinion, I have a package with about ten submodules, and I do not only use the innermost ones inside the outermost ones, but I also import names of the outermost modules to extend them inside the innermost ones. I never had any problem, nor considered stopping my use of submodules.
