# Scope of submodules

**URL:** <https://discourse.julialang.org/t/scope-of-submodules/2806>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 21, 2017, 11:23pm UTC](https://discourse.julialang.org/t/scope-of-submodules/2806 "2017-03-21T23:23:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [March 21, 2017, 11:23pm UTC](https://discourse.julialang.org/t/scope-of-submodules/2806/1 "2017-03-21T23:23:03Z")

</div>

Say I have a module A, in which I define a constant. I would like to be able to use that constant in submodules of that module:

```julia
module A
a = 2
module B
f(x) = x*a
end
module C
g(x) = x*a
end
end

```

This does not work:

```julia
A.B.f(3)
UndefVarError: a not defined

```

Clearly, I need to import `a` into each submodule, but that seems a bit annoying… Would there be a way to automatically inherit from the superscope, possibly with a `submodule` keyword?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 22, 2017, 2:35am UTC](https://discourse.julialang.org/t/scope-of-submodules/2806/2 "2017-03-22T02:35:40Z")

</div>

There isn’t a way (as far as I know) to automatically inherit from the outer scope, but you might find some of the answers from here helpful: [Avoid repeating the same `using` line for enclosed modules](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549)

---

<div class="post-metadata">

**Author:** ![m-j-w](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-j-w/32/4541_2.png) [@m-j-w](https://discourse.julialang.org/u/m-j-w)\
**Post date:** [March 22, 2017, 4:17am UTC](https://discourse.julialang.org/t/scope-of-submodules/2806/3 "2017-03-22T04:17:55Z")

</div>

Try `import ..a`, which however doesn’t free you from importing in general; see also [Global Scope in the manual](http://docs.julialang.org/en/latest/manual/variables-and-scoping.html#Global-Scope-1):

```nohighlight
julia> module A
           a = 2
           module B
               import ..a
               f(x) = x*a
           end
           module C
               import ..a
               g(x) = x+a
           end
       end
       println(A.B.f(3)) # = 6
       println(A.C.g(4)) # = 6
       eval(A, :(a = 5))
       println(A.B.f(3)) # = 15
       println(A.C.g(4)) # = 9
6
6
15
9

```

or alternatively `using A: a` instead of import.

You could also `export a`, then simply `using A`:

```nohighlight
julia> module A
           export a
           a = 2
           module B
               using A
               f(x) = x*a
           end
           module C
               using A
               g(x) = x+a
           end
       end

```

Or, if you have a large number of variables to import and prefer explicitly naming the ones to import, then a simple trick would be to put that using/import statement in another file and include that file at the respective positions.  
That all at least reduces the problem to a one-liner per submodule. Using some other macro or keyword probably isn’t much less typing.
