# Avoid repeating the same \`using\` line for enclosed modules

**URL:** <https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549>\
**Category:** General Usage\
**Tags:** question, modules\
**Created:** [March 8, 2017, 10:43pm UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549 "2017-03-08T22:43:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![xqrongm](https://avatars.discourse-cdn.com/v4/letter/x/d9b06d/32.png) [@xqrongm](https://discourse.julialang.org/u/xqrongm)\
**Post date:** [March 8, 2017, 10:43pm UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/1 "2017-03-08T22:43:12Z")

</div>

I want to write modules inside of a module. Is there a way to avoid repeating the same `using` line of code?

```julia
module Module0

using Pkg1, Pkg2, Pkg3

module Module01
using Pkg1, Pkg2, Pkg3
some_code_1
end

module Module02
using Pkg1, Pkg2, Pkg3
some_code_2
end

end

```

.

---

<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 8, 2017, 11:42pm UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/2 "2017-03-08T23:42:33Z")

</div>

One option would be to use the `@def` macro that @ChrisRackauckas has mentioned here previously:

```julia
 macro def(name, definition)
    return quote
        macro $(esc(name))()
            esc($(Expr(:quote, definition)))
        end
    end
end

```

which you could use like this:

```julia
@def use_packages begin
    using Pkg1, Pkg2, Pkg3
end

module Module01
@use_packages
end

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [March 9, 2017, 5:08am UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/3 "2017-03-09T05:08:40Z")

</div>

Consider instead, not using submodules.  
I wrote a [SO answer about this a while ago.](http://stackoverflow.com/a/40314833/179081)

I find the using nested modules rarely worksout for the best.  
Especially if you are using many of them.

Not a hard-rule.  
But it is good to stop and check that the submodules are doing anything useful for you.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 9, 2017, 6:03am UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/4 "2017-03-09T06:03:14Z")

</div>

I completely agree with this. A simplification:

1. Function names should be short and simple, and should use dispatch in order to distinguish between methods. So they should all be named the same, but dispatch will take care of any possible issues.

2. Types should have unique names. Otherwise it would be confusing, right?

3. Most of Julia is encoded in functions and types.

This is the same reason why `import * from ...` is not good in Python, but exporting stuff from Julia modules is okay. “Clashing” isn’t really a problem if you are doing dispatch on unique (and aptly named) types. This gives a lot less of an incentive to submodulize compared to other languages.

But if you do need to, then yes, the `@def` macro will do compile time copy/pasting for you.

---

<div class="post-metadata">

**Author:** ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)\
**Post date:** [March 9, 2017, 7:45am UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/5 "2017-03-09T07:45:12Z")

</div>

I love submodules. My packages typically use them generously. But if all the submodules are using the same dependencies, that is a strong indicator that perhaps they are more coupled than is typical, and might be better off in the same module.

---

<div class="post-metadata">

**Author:** ![xqrongm](https://avatars.discourse-cdn.com/v4/letter/x/d9b06d/32.png) [@xqrongm](https://discourse.julialang.org/u/xqrongm)\
**Post date:** [March 9, 2017, 5:26pm UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/6 "2017-03-09T17:26:04Z")

</div>

Thank everyone for the helpful comments!

I’m having a discussion with our software lead about how we implement an API in Julia, where users call a function `analyze`. She wants different `analyze` functions that perform different types of analyses, and to avoid namespace issues, she wants me to use modules, i.e. using different module names for different types of analyses, but every such module will have one function called `analyze`. Among the types of analyses we already set up, many of them will have essentially the same dependencies (`using` the same set of packages), meaning they are too coupled to be in separate modules? The issue of using multiple dispatch is that she wants exactly the same function signature for each `analyze` function. So I’m not sure how to propose better alternatives.

Thanks for any potential help!

---

<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 9, 2017, 5:33pm UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/7 "2017-03-09T17:33:30Z")

</div>

One alternative to submodules in your case would be to use an additional argument to distinguish the type of analysis to do. For example:

```julia
abstract AnalysisType

immutable Linear <: AnalysisType
end

immutable Quadratic <: AnalysisType
end

analyze(::Type{Linear}, x) = println("Linear analysis of $x")
analyze(::Type{Quadratic}, x) = println("Quadratic analysis of $x")

```

which you could run like this:

```julia
analyze(Linear, [1,2,3])

```

With a design like this, users can implement their own compatible analysis types by creating a new `AnalysisType` and a new method for the same `analyze()` function.

Or, if you don’t like using types as values, a slightly different signature for `analyze` would be:

```julia
analyze(::Linear, x) = println("Linear analysis of $x")
analyze(::Quadratic, x) = println("Quadratic analysis of $x")

```

which you could use like this:

```julia
analyze(Linear(), [1,2,3])

```

(note the extra `()` after `Linear` in this case).

The [Interpolations.jl](https://github.com/tlycken/Interpolations.jl) package uses a design like this.

---

<div class="post-metadata">

**Author:** ![guixien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guixien/32/34399_2.png) [@guixien](https://discourse.julialang.org/u/guixien)\
**Post date:** [March 8, 2022, 1:28pm UTC](https://discourse.julialang.org/t/avoid-repeating-the-same-using-line-for-enclosed-modules/2549/8 "2022-03-08T13:28:22Z")

</div>

Some modules are just very generic and can be used across computing tasks in sub-modules, e.g. CUDA.jl if you want to utilize the GPU.
