# Modules and namespaces

**URL:** <https://discourse.julialang.org/t/modules-and-namespaces/45027>\
**Category:** General Usage\
**Tags:** namespaces\
**Created:** [August 16, 2020, 7:04am UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027 "2020-08-16T07:04:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Holdmycoffee](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@Holdmycoffee](https://discourse.julialang.org/u/Holdmycoffee)\
**Post date:** [August 16, 2020, 7:04am UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027/1 "2020-08-16T07:04:44Z")

</div>

Hey everyone, so I have a weird namespace issue. I import a struct “parameter” into a module QuadratureTypes, then import a function which takes an instance of parameter as input from QadratureTypes into another module, where I attempt to use it.

```julia
# parameters.jl
module Parameters
abstract type Parameter end
struct parameter <: Parameter
...

```

```julia
module QuadratureTypes
include("../../core/parameters.jl")
using .Parameters: parameter, Parameter
...

```

```julia
include("../../../src/core/parameters.jl")
using .Parameters: Parameter, parameter
function quad_info(p:: parameter) 
...

```

However when I try to use the function quad\_info, it complains of an unmatched input arg.

```julia
quad_info(!Matched::Main.QuadratureTypes.Parameters.parameter)

```

The type of the input I am passing in is the following

```julia
parameter

```

How did these two versions of `parameter ` come about, and did I do something wrong in my imports? Why is there both `parameter ` and `Main.QuadratureTypes.Parameters.parameter` instead of a single one?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 16, 2020, 9:51am UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027/2 "2020-08-16T09:51:03Z")

</div>

My guess is, you have a

```julia
using QuadratureTypes

```

and a

```julia
include("...somepath...parameters.jl")

```

in your main for your function `quad_info` to know about `parameter`.

Try to remove the include and refer to the types `parameter` and `Parameter` only with  
`QuadratureTypes.Parameters` and `QuadratureTypes.parameters`.

Does this make sense?

By the way: the convention is to make types starting with uppercase. So it would be, if you like to follow the convention, `Parameter` and e.g.

```julia
struct ParameterStruct <: Parameter
...

```

---

<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:** [August 16, 2020, 4:07pm UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027/3 "2020-08-16T16:07:47Z")

</div>

Perhaps this is helpful (really just explaining oheil’s answer a bit): If you include the same file twice, it usually indicates a problem.  
In this case, you seem to include “parameters.jl” in `Main` and in `QuadratureTypes`.  
This results in `Main.Parameter` being different from `Main.QuadratureTypes.Parameter`.

---

<div class="post-metadata">

**Author:** ![Holdmycoffee](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@Holdmycoffee](https://discourse.julialang.org/u/Holdmycoffee)\
**Post date:** [August 16, 2020, 8:31pm UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027/4 "2020-08-16T20:31:22Z")

</div>

Thanks for your replies @hendri54 and @oheil. The naming convention has been noted. However, if including the same file twice poses a problem, then how do I go about reusing a struct from a module? I have a file parameters.jl containing the module Parameters containing the struct `parameter`. In order to use the struct `parameter` in other modules, it appears that I have to first do `include(...parameters.jl)` followed by either `using` or `import .Parameters: parameter` inside of the module. Then when I export functions from these modules, they aren’t compatible with the original `parameter` type, but rather a local copy of `parameter` imported in the scope of the module.

If I remove the include at the start of the module, I can’t do using `.Parameters: parameter, Parameter` because Parameters is not defined.

---

<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:** [August 16, 2020, 8:44pm UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027/5 "2020-08-16T20:44:34Z")

</div>

See [Need to include one file multiple times, how to avoid warning - #5 by rdeits](https://discourse.julialang.org/t/need-to-include-one-file-multiple-times-how-to-avoid-warning/27439/5) or [Dependencies of src files inside a package - #2 by rdeits](https://discourse.julialang.org/t/dependencies-of-src-files-inside-a-package/43144/2) for how to avoid including the same file multiple times, or see [Trouble importing a module that depends on another globally imported module - #7 by rdeits](https://discourse.julialang.org/t/trouble-importing-a-module-that-depends-on-another-globally-imported-module/26482/7) or [Error with modules sharing types - #4 by rdeits](https://discourse.julialang.org/t/error-with-modules-sharing-types/37119/4) for how to avoid needing `include` at all.

---

<div class="post-metadata">

**Author:** ![Holdmycoffee](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@Holdmycoffee](https://discourse.julialang.org/u/Holdmycoffee)\
**Post date:** [August 16, 2020, 9:12pm UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027/6 "2020-08-16T21:12:10Z")

</div>

Thanks for these links @rdeits! They solved my problem. However, I wonder what implications this has for testing. If the accepted style is to `includes()` separate files into a monolithic module (albeit using few lines of code), the the test file for that monolithic file will be large. From what I’ve seen, a lot of Julia packages have test files which are \>2000 LOC, because the individual files which get included in the module can’t be tested on their own, as they aren’t standalone, so to speak. This is unlike what I’ve seen in Python packages.

---

<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:** [August 16, 2020, 9:36pm UTC](https://discourse.julialang.org/t/modules-and-namespaces/45027/7 "2020-08-16T21:36:28Z")

</div>

Even if all the code to be tested lives in a single module, nothing prevents you from structuring the tests so that tests for subsets of the code can be run independently.  
I typically have one big `@testset` in `runtests.jl` that just consists of a bunch of include statements. In your case, you might `include("parameters_test.jl")`, which in turn consists of a `@testset` that calls a bunch of functions. Each function contains yet another `@testset` the focuses on one feature of the code.  
That way I can `include("test/parameters_test.jl")` from the REPL to just run those tests. And the functions provide some isolation of the tests from each other.  
Perhaps I am overcomplicating things this way, but it has worked well for me.
